other#nginx配置


#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;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    # proxy_pass 和 fastcgi_pass中会用到 upstream定义的代理服务器
    # upstream 中可以定义多个server 代理服务器
    upstream vue{
        server localhost:8081;
    }

    server {
        listen       8000;
        server_name  localhost;
        #access_log  logs/host.access.log  main;

        location / {
            proxy_set_header        Host $host;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_pass                 http://vue;
        }

        #proxy_intercept_errors 针对location匹配后的结果是proxy_pass后的错误处理
        proxy_intercept_errors  on;
        error_page  404              /404.html;
        error_page  500 502 503 504  /50x.html;

        location = /50x.html {
            root   html;
        }
        location = /404.html {
            root   html;
        }
        
        location  /term {
            proxy_http_version      1.1;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        X-Real-IP $remote_addr;

            #完全可以自定义一个http header,在java中通过HttpServletRequest.getHeader(headerName)
            #就可以获取到自定义头的值
            #语法是 proxy_set_header headerName headerValue;
            #如果headerValue不是nginx提供的变量,而是字符串,就通过单引号给引起来
            proxy_set_header        luo-hao-nan 'luohaonan';
            proxy_pass              http://localhost:8082/;
        }
    }

}

# c:\nginx-1.16.0>nginx -h
# nginx version: nginx/1.16.0
# Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

# Options:
#   -?,-h         : this help
#   -v            : show version and exit
#   -V            : show version and configure options then exit
#   -t            : test configuration and exit
#   -T            : test configuration, dump it and exit
#   -q            : suppress non-error messages during configuration testing
#   -s signal     : send signal to a master process: stop, quit, reopen, reload
#   -p prefix     : set prefix path (default: NONE)
#   -c filename   : set configuration file (default: conf/nginx.conf)
#   -g directives : set global directives out of configuration file


# nginx简单配置思路
# 围绕着location展开,对匹配的路径进行 [被请求内容] 定位
#   [被请求内容] 可以来自本地文件系统,由root指令指定[被请求内容]的存放位置
#   [被请求内容] 也可以来自proxy_pass指令指定的地址,此时nginx将把请求转发到proxy_pass指令指定的地址

# proxy_pass指令后面的指定的地址 可以直接是一个url比如[http://localhost:8082/]
# proxy_pass指令后面的指定的地址 也可以是upstream定义的一组url的名称入上面的vue,此时请求被分发到vue对应的一组url上面

# 通过proxy_pass后在后端服务接口会丢失部前端用户的信息,比如说ip地址。为了解决这个问题,需要使用proxy_set_header指令,
# 通过proxy_set_header指令设置头信息,让头中包含自定义的信息,然后再后端服务接口中在手动取出对应的头信息就ok了,
# 比如所针对nginx代理后服务端接口使用java的情况下,HttpServletRequest.getRemoteAddr()获取到的是nginx的地址,而不是真实的
# 客户浏览器的地址,那么通过在location中设置proxy_set_header X-Real-IP $remote_addr;就可以在后端服务接口中通过header获取
# 客户的真实ip了。HttpServletRequest.getHeader("X-Real-IP").需要注意的是header名称是任意的,也可以设置成其他的名称,
# 要灵活的操作就ok了。

# 如果使用proxy_pass,同样想使用error_page功能,那么需要设置proxy_intercept_errors为on


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM