本地配置Ngnix实现反向代理

1.Ngnix文件详情

#user  nobody;
worker_processes  1;  #工作进程数

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024; #最大连接数
}


http {
    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        on; #是否调用sendfile函数来输出文件,减少用户空间到内核空间的上下文切换。
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;  #长连接超时时间,单位是秒
    client_max_body_size 10M; #允许客户端请求的最大单文件字节数。
    client_body_buffer_size 128k   #缓冲区代理缓冲用户端请求的最大字节数


   
    gzip on; #gzip压缩功能设置
    gzip_min_length 1k; #设置允许压缩的页面最小字节数,默认值是20,小于1k可能会越压越大。
    gzip_buffers 4 16k; #设置系统获取单位存储压缩数据流, 4 16K代表,数据大小以16k为单位的4倍 
    申请内存
    gzip_http_version 1.0; #设置http协议版本
    gzip_comp_level 6; #设置gzip压缩速度,1处理速度最快 9压缩速度最慢。
    gzip_types text/html text/plain text/css text/javascript application/json 
    application/javascript application/x-javascript application/xml;#匹配是什么类型进行压缩
    gzip_vary on; #在响应头加上Vary:Accept-Encoding,可以让前端的缓存服务器缓存经过gzip压缩的 
    页面
     
    client_max_body_size 10m;#允许客户端请求的最大单文件字节数。
    client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数


    # http_proxy 设置
    proxy_connect_timeout 75; #nginx跟后端服务器连接超时时间(代理连接超时)

    proxy_send_timeout 75;  #指定响应客户端的超时时间

    proxy_read_timeout 75; #连接成功后与服务器响应操作之间的超时时间(代理接收超时)

proxy_buffer_size 4k; #设置代理服务器(nginx)从后端realserver读取并保存用户头信息的缓冲区大小
    proxy_buffers 4 32k; #缓存区大小

    proxy_busy_buffers_size 64k; #高负荷下缓冲区大小

    proxy_temp_file_write_size 64k;#当缓存被代理的服务器响应到临时文件时,这个选项限制每次写临时文件的大小。
     proxy_max_temp_file_size #当放不下内容的时候,设置最大临时文件大小

    proxy_temp_path /usr/local/nginx/proxy_temp 1 2;

   # 设定负载均衡后台服务器列表
      upstream itlinli.gmall.com { 
       # server 192.168.2.160:8091;
       # server 192.168.2.160:8092;
       # server 192.168.2.160:8093;
        server  172.16.32.180:8099;
        }
 
       #虚拟主机配置
        server {
        listen       80;  #监听80也就是监听127.0.0.1/的请求
        server_name  itlinli.gmall.com;  #监听的ip地址或者域名,可以通过正则匹配
		
        location / {    #表示然后匹配itlinli.gmall.com/***后面的请求路径
         proxy_pass http://itlinli.gmall.com;  #反向代理到那个服务器上去,对应upstream 中的 
                                                                      #itlinli.gmall.com
         proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
        }

        #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;
        }
     
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {  #location /也是匹配访问目录下的那个文件夹。
    #        root   html;   #默认在nginx/html下下面的 
    #        index  index.html index.htm;   #定义路径下默认访问的文件名
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

2.location配置详情

      语法详情,语法规则:location [=|~|~*|^~] /uri/ { … }

      = 开头表示精确匹配

  ^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规        则^~ /static/ /aa匹配到(注意是空格)。以xx开头

  ~ 开头表示区分大小写的正则匹配                     以xx结尾

  ~* 开头表示不区分大小写的正则匹配                以xx结尾

  !~!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

  / 通用匹配,任何请求都会匹配到。

location = / {
   #规则A
}
location = /login {
   #规则B
}
location ^~ /static/ {
   #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #规则D,注意:是根据括号内的大小写进行匹配。括号内全是小写,只匹配小写
}
location ~* \.png$ {
   #规则E
}
location !~ \.xhtml$ {
   #规则F
}
location !~* \.xhtml$ {
   #规则G
}
location / {
   #规则H
}

  访问根目录/, 比如http://localhost/ 将匹配规则A

  访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H

  访问 http://localhost/static/a.html 将匹配规则C

  访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而                        http://localhost/static/c.png 则优先匹配到 规则C

  访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。

  访问 http://localhost/a.xhtml 不会匹配规则F和规则G,

  http://localhost/a.XHTML不会匹配规则G,(因为!)。规则F,规则G属于排除法,符合匹配规则也不会匹配到,所以想想看实    际应用中哪里会用到。

  访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配

3.配置负载均衡

  upstream itlinli.gmall.com{     #默认轮询
        server 192.168.2.160:8091;  
        server 192.168.2.160:8092;
        server 192.168.2.160:8093;
        }
    server {
        listen       80;
        server_name  itlinli.gmall.com;
		
        location / {
         proxy_pass http://itlinli.gmall.com;
         proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
        }

第一次访问http://itlinli.gmall.com会转发到192.168.2.160:8091中,监听80端口就是http://itlinli.gmall.com/   名字必须匹配一样。

猜你喜欢

转载自blog.csdn.net/qq_41085151/article/details/107517938