Nginx.conf反向代理配置详解

来源:互联网 发布:行业研究的数据来源 编辑:程序博客网 时间:2024/06/06 00:05


#user  nobody;
worker_processes  1;#设置nginx服务的worker子进程,比如设为2:


error_log  logs/error.log; #去掉前面的#,记录nginx错误日志,方便检查bug:
error_log  logs/error.log  notice;
error_log  logs/error.log  info;


#pid        logs/nginx.pid;  #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;
#这里很关键,很多小伙伴问我 “负载均衡乍配置,为啥我配置的不能访问呢“,这里的upstream就是配置负载均衡的,当然得两台以上才叫负载,我这里的ip69和68都是
#用的apache,   也许你们的是tomcat, 没关系,按这样配置一样可以,
 upstream proxy_test {
   server 127.0.0.1:8080 weight=1;     #如果你要测试,把这里换成你自己要代理后端的ip
   server 127.0.0.1:8081 weight=1;
   ip_hash;                                              #当负载两台以上用ip_hash解决session的问题,一台就别hash了。
 }
#这是server段的配置
    server {
        listen       80;
        server_name  localhost;#要访问的域名,我这里用的测试域名,如果有多个,用逗号分开


        charset utf8;


        #access_log  logs/host.access.log  main;


        location / {
           # root   html;
           # index  index.html index.htm;
    proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://proxy_test;  #这里proxy_test是上面的负载的名称,映射到代理服务器,可以是ip加端口,   或url 
        }
access_log logs/manageplatform.log;


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


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }




    # 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 / {
    #        root   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;
    #    }
    #}


}