nginx配置文件示例

1、nginx作为静态资源服务器配置

location ^~/ceng/ {
      alias  F:/html/html2/;
     #add_header Cache-Control no-store;
     #add_header expire -1;
}
location ^~/hehe/ {
     alias  F:/html/;
    #add_header Cache-Control no-store;
    #add_header expire -1;
}

 强调 location 后的 "^~" 顺序不能反了

 

2、nginx作为反向代理服务器配置

location ~ ^/test/{
     proxy_pass http://hello;
     proxy_read_timeout 180;
     client_max_body_size    1000m;
     proxy_redirect http://hello http://127.0.0.1:8001;

}

proxy_redirect参数说明:

     这个参数只在发生重定向时生效,

  前提:proxy_redirect http://hello http://127.0.0.1:8001; 其中 http://hello 为param1,http://127.0.0.1:8001为param2, host为http/https+IP+端口号

  param1与proxy_pass后的参数保持一致,param2是对重定向后的url的补充(第二次请求格式:host+param2+重定向url),如param2中有host(第二次请求格式:param2+重定向url

 

3、nginx做负载均衡配置

upstream hello{
    server 10.164.197.56:8080 weight=10;
    server 10.164.197.57:8080 weight=10;
}

 说明:上面第二部分的proxy_pass后的 和第二部分的upstream 后的要一致,一致的规则如上面的示例。

 强调:作为反向服务器的location 后面的 "~ ^" 与作为静态资源服务器正好反过来

 

以下是nginx.conf文件的完整示例:设置编码(charset),配置文件的每一项最后要加上 ";" ,否则启动报错

#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;
	charset utf-8;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	upstream hello{
        server localhost:8000  weight=10;
	server localhost:8001  weight=10;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
		
		
        #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       8001;
        server_name  127.0.0.1;

    #    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 ~ ^/test/{
            proxy_pass http://hello;
	        proxy_read_timeout 180;
            client_max_body_size    1000m;
            proxy_redirect http://hello http://127.0.0.1:8001;

        }
	
	    location ^~/ceng/ {
            alias  F:/html/html2/;
            #add_header Cache-Control no-store;
            # add_header expire -1;
        }
		location ^~/hehe/ {
            alias  F:/html/;
            #add_header Cache-Control no-store;
            # add_header expire -1;
        }
		
    }

}

 

posted @ 2019-02-22 15:33  品书读茶  阅读(583)  评论(0)    收藏  举报