nginx主页:http://nginx.org/
nginx for windows:http://nginx.org/en/docs/windows.html
学习nginx:http://wiki.nginx.org/GettingStarted
以下为location的demo,汇总常见配置以及它们的对比:
demo1(匹配度由低到高):
#1 location / { return 500; } #2 location /a/ { return 404; } #3 location ~* \.jpg$ { return 403; } #4 location ^~ /a/ { return 402; } #5 location /a/1.jpg { return 401; } #6 location = /a/1.jpg { return 400; }
demo2(关于安全性):
location ~* \.php$ { proxy_pass http://www.baidu.com; } location ^~ /upload/ { if ($request_filename ! ~* \.(jpg|jpeg|gif|png|swf|zip|rar|txt)$) { return 403; } alias /home/www/html/upload/; }
demo3(正则按顺序匹配):
location ~* \.jpg$ { return 402; } location ~* 1\.jpg$ { return 403; }
实例一:
news.maijia.com下的请求归档到www.maijia.com/news下
#设定负载均衡的服务器列表 upstream maijia_servers { server 127.0.0.1:8388 weight=5 max_fails=2 fail_timeout=600s; server 127.0.0.1:8389 weight=5 max_fails=2 fail_timeout=600s; } server { listen 80; server_name maijia.com www.maijia.com; index index.html index.htm index.jsp index.php; location = / { proxy_pass http://maijia_servers; } location ~ ^(/bbjk|/dpjk|/btyh|/ztc|/hqfx|/aboutus) { proxy_pass http://maijia_servers; } location ~ /news/.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { rewrite ^/news/(.*)$ /$1 break; root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\news\\news-rest-web\\src\\main\\webapp; expires 1d; } location ~ /news/.+\.(js|css|less)$ { rewrite ^/news/(.*)$ /$1 break; root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\news\\news-rest-web\\src\\main\\webapp; expires 2h; } location ~* ^.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\api\\api-rest-web\\src\\main\\webapp; expires 1d; } location ~* ^.+\.(js|css|less)$ { root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\api\\api-rest-web\\src\\main\\webapp; expires 2h; } location ~ /news { rewrite ^/news(.*)$ /$1 break; proxy_pass http://127.0.0.1:8688; 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; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 120; proxy_read_timeout 120; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; add_header Pragma "no-cache"; add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; #proxy_redirect http://127.0.0.1:8688 http://www.maijia.com; } # location ~* ^.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { # root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\news\\news-rest-web\\src\\main\\webapp; # expires 1d; #} #location ~* ^.+\.(js|css|less)$ { # root D:\\workspace-sts-3.5.1.RELEASE\\maijia\\news\\news-rest-web\\src\\main\\webapp; # expires 2h; # } location ~* / { proxy_pass http://127.0.0.1:7677; 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; client_max_body_size 10m; client_body_buffer_size 1128k; proxy_connect_timeout 11190; proxy_send_timeout 11120; proxy_read_timeout 11120; proxy_buffer_size 1114k; proxy_buffers 4 1132k; proxy_busy_buffers_size 1164k; proxy_temp_file_write_size 1164k; proxy_ignore_client_abort on; add_header Pragma "no-cache"; add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; } }
server { listen 80; server_name news.maijia.com; index index.html index.htm index.jsp index.php; #location ~* ^.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { # root /data/project/maijia_app/code; # expires 1d; #} #location ~* ^.+\.(js|css|less)$ { # root /data/project/maijia_app/code; # expires 2h; # } if ($host ~* ^news.maijia.com$) { rewrite ^/(.*) http://www.maijia.com/news/$1 permanent; } #location ~* / { # proxy_pass http://127.0.0.1:8688; # 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; # client_max_body_size 10m; # client_body_buffer_size 1128k; # proxy_connect_timeout 11190; # proxy_send_timeout 11120; # proxy_read_timeout 11120; # proxy_buffer_size 1114k; # proxy_buffers 4 1132k; # proxy_busy_buffers_size 1164k; # proxy_temp_file_write_size 1164k; # proxy_ignore_client_abort on; # add_header Pragma "no-cache"; # add_header Cache-Control "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"; # } }
实例二:
访问cdn.maijia.com/images/跳转到img.maijia.com
server { listen 80; server_name cdn.maijia.com; index index.html index.htm index.jsp index.php; location ~ /images/.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { rewrite ^/images/(.*)$ http://img.maijia.com/$1 permanent; } location ~* ^.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { root D:\workspace-sts-3.5.1.RELEASE\fe\cdn; expires 1d; } location ~* ^.+\.(js|css|less)$ { root D:\workspace-sts-3.5.1.RELEASE\fe\cdn; expires 2h; } }
server { listen 80; server_name img.maijia.com; index index.html index.htm index.jsp index.php; location ~* ^.+\.(log|jpg|jpeg|gif|png|ico|html|cfm|cfc|afp|asp|lasso|pl|py|txt|fla|swf|zip)$ { root D:\workspace-sts-3.5.1.RELEASE\fe\img; expires 1d; } location ~* ^.+\.(js|css|less)$ { root D:\workspace-sts-3.5.1.RELEASE\fe\img; expires 2h; } }
其他blog:
http://www.linuxidc.com/Linux/2014-01/95493.htm