Nginx反向代理以及配置优化

下面配置包含了,nginx配置的一个比较全面的反向代理的例子:

user www www
worker_processes  8;



events {
    use epoll;
    worker_connections  10240;
}

pid       /opt/work/log/nginx.pid;
error_log /dev/null error;

http {

    include /opt/nginx/conf/mime.types;
    default_type     text/html;

     log_format  pv  '[$time_local]|$remote_addr|$cookie_SUV|$args|$http_referer|$http_user_agent';

     log_format   main '$request_time $remote_addr $cookie_SUV [$time_local]  $status '  '"$request" $body_bytes_sent "$http_referer" '  '"$http_user_agent" "$http_x_forwarded_for"'  '"$cookie_Fr"';
    
     log_format  pbmain    '$remote_addr - $remote_user [$time_local] "$request" '
                                '$status $body_bytes_sent "$http_user_agent"';
     access_log off;

     sendfile    on;


     keepalive_timeout  0;

    gzip  on;
    
    #允许客户端请求的最大单个文件字节数
    client_max_body_size    200m;
    upload_progress proxied 8m;
    
    #DNS服务器ip
    resolver 192.168.1.1;
    
    proxy_set_header Host $http_host;
    proxy_set_header x-forwarded-for  $remote_addr;
    proxy_cache_key  "$scheme$host$request_uri";
    proxy_cache_path /opt/work/cache levels=1:2:1 keys_zone=js_cache:100m max_size=1024m inactive=15d;
    proxy_cache_valid any 15d;

    upstream backend {
        ip_hash;
        server  127.0.0.1:8080 weigth=4 max_fails=2 fail_timeout=30s;
        server  127.0.0.1:8081 weigth=3 max_fails=2 fail_timeout=30s;
    }
    upstream liveBackends {
       server   192.168.1.15:9090;
       server   192.168.1.16:9090;
       #备机
       server   192.168.1.17:9090 backup;
     }

    server{
        listen 80;
        server_name www.myserver1.com;
    	error_log   /opt/work/log/nginx_error.log error;
        client_max_body_size    200m;
        proxy_set_header Host $http_host;
        proxy_set_header x-forwarded-for  $remote_addr;
        proxy_buffer_size         64k;
        proxy_buffers             32 64k;
    
        #查看服务状态
        location /server-status {
               stub_status on;
               access_log   off;
               allow all;
        }
        location ^~ /pv
        {
            access_log  /opt/work/log/nginx_access.log  pv;
            proxy_pass http://backend;
        }
  
        location ^~ /cc.do
        {
            proxy_pass http://backend;
        }
        location ^~/favicon.ico{
            alias /usr/local/src/web/favicon.ico;
         }

       location ^~ /pvlog
        {
            access_log  /opt/work/log/pv/nginx_access.log  pv;
	    access_log  /opt/work/log/main/nginx_access_main.log pv_main;
            error_log   /opt/work/log/error/nginx_error.log error;
            return 204;
        }
    
       location /proxy/
        {
	    #后端返回502,504,执行超时等错误,自动将请求转发到upstream负载池中另一台服务器
            proxy_next_upstream  http_502 http_504 error timeout invalid_header;
	    proxy_pass http://backend;  
        }
        #禁止访问除以外的其他目录
        location /{
           deny all;
        }
    }

     server{
        listen       80;
        server_name  www.myserver2.com;
        root         /www; 
	location ~ t1\.(gif)$ {
		root /www;
		expires -1;
                access_log      /data/log/access.log  pbmain;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            
	}

        location ~ t2.(gif)$ {
                root /www;
                expires -1;             
                access_log      /data/log/access.log  pbmain;
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
             if ($args ~ "ltype=1(&|$)") { 	 							
  		proxy_pass http://liveBackends;
	     }
        }

       
	location /server-status {
                        stub_status     on;
                        access_log      off;
                        allow           10.0.0.0/8;
                        allow           192.168.0.0/16;
                        deny            all;
         }
        error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}


作者:liuzhoulong 发表于2013-5-5 13:48:26 原文链接
阅读:0 评论:0 查看评论

你可能感兴趣的:(nginx,反向代理,优化)