nginx深刻使用

 1. nginx使用命令

nginx -s stop       --快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit       --平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload     --因改变了Nginx相关配置,须要从新加载配置而重载。
nginx -s reopen     --从新打开日志文件。
nginx -c filename   --为 Nginx 指定一个配置文件,来代替缺省的。
nginx -t            --不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
nginx -v            --显示 nginx 的版本。
nginx -V            --显示 nginx 的版本,编译器版本和配置参数。

2. nginx配置文件内容详析

基于http反向代理的配置javascript

#运行用户
#user  nobody;

#启动进程,一般设置成和cpu的数量相等
worker_processes  1;

#全局错误日志
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#PID文件,记录当前启动的nginx的进程ID
#pid        logs/nginx.pid;

#工做模式及链接数上限
events {
    worker_connections  1024;    #单个后台worker process进程的最大并发连接数
}

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #设定mime类型(邮件支持类型),类型由mime.types文件定义
    include       mime.types;
    default_type  application/octet-stream;

    #设定日志
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
    #必须设为 on,若是用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,下降系统的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #链接超时时间
    keepalive_timeout  65;

    #gzip压缩开关
    #gzip  on;

    #设定实际的服务器列表,可设置权重
    upstream server.lb{
        server localhost:8089 weight=10;
        server localhost:8090 weight=5;
        ip_hash;
    }
    
    #http服务器
    server {
        #监听80端口,80端口是知名端口号,用于HTTP协议
        listen       80;
        
        #定义使用localhost访问,当有具体的域名可使用具体域名如www.xx.com
        server_name  localhost;

        #编码格式
        charset utf-8;

        #access_log  logs/host.access.log  main;
    
        location / {
            root   html;   #静态文件,nginx本身处理
            proxy_pass http://server.lb;   #反向代理的路径(和upstream绑定),location 后面设置映射的路径
            index  index.html index.htm;
        }

        #错误处理页面4xx,5xx(可选择性配置)
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}}

3. 实际经常使用的负载均衡配置

3.1 多个相同服务的负载均衡

如 nginx安装及配置 中,将应用部署在同一机器上的多个tomcat中,经过不一样端口访问造成的伪集群,在实际运营中为防止单台机器挂掉致使应用不可访问,一般是有多台服务器运行着一样的服务。css

如:将应用部署在 192.168.1.11:80、192.168.1.12:80、192.168.1.13:80 三台 linux 环境的服务器上。网站域名叫 www.helloworld.com,公网 IP 为 192.168.1.11。在公网 IP 所在的服务器上部署 nginx,对全部请求作负载均衡处理。html

nginx.conf 配置以下:前端

http {
     #设定mime类型,类型由mime.type文件定义
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #设定日志格式
    access_log    /var/log/nginx/access.log;

    #设定负载均衡的服务器列表
    upstream load.balance.server {
        #weigth参数表示权值,权值越高被分配到的概率越大
        server 192.168.1.11:80   weight=5;
        server 192.168.1.12:80   weight=1;
        server 192.168.1.13:80   weight=6;
    }

   #HTTP服务器
   server {
        #侦听80端口
        listen       80;

        #定义使用www.xx.com访问
        server_name  www.helloworld.com;

        #对全部请求进行负载均衡请求
        location / {
            root        /root;                 #定义服务器的默认网站根目录位置
            index       index.html index.htm;  #定义首页索引文件的名称
            proxy_pass  http://load.balance.server ;#请求转向load_balance_server 定义的服务器列表

            #如下是一些反向代理的配置(可选择性配置)
            #proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            #后端的Web服务器能够经过X-Forwarded-For获取用户真实IP
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_connect_timeout 90;          #nginx跟后端服务器链接超时时间(代理链接超时)
            proxy_send_timeout 90;             #后端服务器数据回传时间(代理发送超时)
            proxy_read_timeout 90;             #链接成功后,后端服务器响应时间(代理接收超时)
            proxy_buffer_size 4k;              #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k如下的话,这样设置
            proxy_busy_buffers_size 64k;       #高负荷下缓冲大小(proxy_buffers*2)
            proxy_temp_file_write_size 64k;    #设定缓存文件夹大小,大于这个值,将从upstream服务器传

            client_max_body_size 10m;          #容许客户端请求的最大单文件字节数
            client_body_buffer_size 128k;      #缓冲区代理缓冲用户端请求的最大字节数
        }
    }
}

3.2 分布式应用的负载均衡

当一个网站功能愈来愈丰富时,每每须要将一些功能相对独立的模块剥离出来,独立维护。这样的话,一般,会有多个 webapp。html5

举个例子:假如 www.helloworld.com 站点有好几个 webapp,finance(金融)、product(产品)、admin(用户中心)。访问这些应用的方式经过上下文(context)来进行区分:java

www.helloworld.com/finance/linux

www.helloworld.com/product/nginx

www.helloworld.com/admin/web

咱们知道,http 的默认端口号是 80,若是在一台服务器上同时启动这 3 个 webapp 应用,都用 80 端口,确定是不成的。因此,这三个应用须要分别绑定不一样的端口号。shell

那么,问题来了,用户在实际访问 www.helloworld.com 站点时,访问不一样 webapp,总不会还带着对应的端口号去访问吧。因此,你再次须要用到反向代理来作处理。

http {
    #此处省略一些基本配置

    upstream product.server{
        server www.helloworld.com:8081;
    }

    upstream admin.server{
        server www.helloworld.com:8082;
    }

    upstream finance.server{
        server www.helloworld.com:8083;
    }

    server {
        #此处省略一些基本配置
        #默认指向product的server
        location / {
            proxy_pass http://product.server;
        }

        location /product/{
            proxy_pass http://product.server;
        }

        location /admin/ {
            proxy_pass http://admin.server;
        }

        location /finance/ {
            proxy_pass http://finance.server;
        }
    }
}

3.3 https反向代理

一些对安全性要求比较高的站点,可能会使用 HTTPS(一种使用 ssl 通讯标准的安全 HTTP 协议)。

使用 nginx 配置 https 须要知道几点:

  • HTTPS 的固定端口号是 443,不一样于 HTTP 的 80 端口
  • SSL 标准须要引入安全证书,因此在 nginx.conf 中你须要指定证书和它对应的 key

配置以下

#HTTP服务器
  server {
      #监听443端口。443为知名端口号,主要用于HTTPS协议
      listen       443 ssl;

      #定义使用www.xx.com访问
      server_name  www.helloworld.com;

      #ssl证书文件位置(常见证书文件格式为:crt/pem)
      ssl_certificate      cert.pem;
      #ssl证书key位置
      ssl_certificate_key  cert.key;

      #ssl配置参数(选择性配置)
      ssl_session_cache    shared:SSL:1m;
      ssl_session_timeout  5m;
      #数字签名,此处使用MD5
      ssl_ciphers  HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers  on;

      location / {
          root   /root;
          index  index.html index.htm;
      }
  }

3.4 静态站点配置

有时候,咱们须要配置静态站点(即 html 文件和一堆静态资源)。

举例来讲:若是全部的静态资源都放在了  /app/dist 目录下,咱们只须要在 nginx.conf 中指定首页以及这个站点的 host 便可。

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    gzip on;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript image/jpeg image/gif image/png;
    gzip_vary on;

    server {
        listen       80;
        server_name  static.zp.cn;

        location / {
            root /app/dist;
            index index.html;
            #转发任何请求到 index.html
        }
    }
}

3.5 跨域问题

跨域是指a页面想获取b页面资源,若是a、b页面的协议、域名、端口、子域名不一样,所进行的访问行动都是跨域的,而浏览器为了安全问题通常都限制了跨域访问,也就是不容许跨域请求资源。

浏览器跨域的解决方式有不少种:

  • 1. 客户端 jsonp 须要目标服务器配合一个callback函数。
  • 2. window.name+iframe 须要目标服务器响应window.name。
  • 3. window.location.hash+iframe 一样须要目标服务器做处理。
  • 4. html5的 postMessage+ifrme 这个也是须要目标服务器或者说是目标页面写一个postMessage,主要侧重于前端通信。
  • 5. CORS  须要服务器设置header :Access-Control-Allow-Origin。
    • vi /etc/nginx/nginx.conf
    • http {
        ###start####
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Headers X-Requested-With;
        add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
        ###end ###
      }
  • 6. nginx反向代理 这个方法通常不多有人说起,可是他能够不用目标服务器配合,不过须要你搭建一个中转nginx服务器,用于转发请求。
    • 主要配置以下
    • 经过A网站(部署于http://localhost:81)访问B网站(部署于http://localhost:82)信息,A网站中直接访问    http://localhost:82/api/values 会失败,须要在nginx.conf中进行以下配置,以后经过访问该中转服务器的地址 /apis/api/values 访问便可。
    • server {
              listen       80; #监听80端口,能够改为其余端口
              server_name  localhost; # 当前服务的域名
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location / {
                  proxy_pass http://localhost:81;
                  proxy_redirect default;
              }
      
              location /apis { #添加访问目录为/apis的代理配置
                  rewrite  ^/apis/(.*)$ /$1 break;
                  proxy_pass   http://localhost:82;
             }

       

解释以下:

  • 由配置信息可知,咱们让nginx监听localhost的80端口,网站A与网站B的访问都是通过localhost的80端口进行访问;
  • 咱们特殊配置了一个“/apis”目录的访问,而且对url执行了重写,最后使以“/apis”开头的地址都转到“http://localhost:82”进行处理;
  • rewrite  ^/apis/(.*)$ /$1 break; 表明重写拦截进来的请求,而且只能对域名后边以“/apis”开头的起做用,例如www.a.com/apis/msg?x=1重写。只对/apis重写。
    • rewrite后面的参数是一个简单的正则 ^/apis/(.*)$ ,$1表明正则中的第一个(),$2表明第二个()的值,以此类推。
    • break表明匹配一个以后中止匹配。

 

转自:

http://www.javashuo.com/article/p-usyhmvsx-cr.html

http://www.javashuo.com/article/p-xnqelzeg-kq.html

参考:cors解释

https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS

相关文章
相关标签/搜索