location = / { # 精確匹配 / ,主機名後面不能帶任何字符串 [ configuration A ] } location / { # 由於全部的地址都以 / 開頭,因此這條規則將匹配到全部請求 # 可是正則和最長字符串會優先匹配 [ configuration B ] } location /documents/ { # 匹配任何以 /documents/ 開頭的地址,匹配符合之後,還要繼續往下搜索 # 只有後面的正則表達式沒有匹配到時,這一條纔會採用這一條 [ configuration C ] } location ~ /documents/Abc { # 匹配任何以 /documents/ 開頭的地址,匹配符合之後,還要繼續往下搜索 # 只有後面的正則表達式沒有匹配到時,這一條纔會採用這一條 [ configuration CC ] } location ^~ /images/ { # 匹配任何以 /images/ 開頭的地址,匹配符合之後,中止往下搜索正則,採用這一條。 [ configuration D ] } location ~* \.(gif|jpg|jpeg)$ { # 匹配全部以 gif,jpg或jpeg 結尾的請求 # 然而,全部請求 /images/ 下的圖片會被 config D 處理,由於 ^~ 到達不了這一條正則 [ configuration E ] } location /images/ { # 字符匹配到 /images/,繼續往下,會發現 ^~ 存在 [ configuration F ] } location /images/abc { # 最長字符匹配到 /images/abc,繼續往下,會發現 ^~ 存在 # F與G的放置順序是沒有關係的 [ configuration G ] } location ~ /images/abc/ { # 只有去掉 config D 纔有效:先最長匹配 config G 開頭的地址,繼續往下搜索,匹配到這一條正則,採用 [ configuration H ] } location ~* /js/.*/\.js
實際在用的配置php
#user nobody; worker_processes 2; #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; #服務器的集羣 #upstream localhost{ #服務器集羣名字 #server localhost:80;#服務器配置 weight是權重的意思,權重越大,分配的機率越大。 #server 10.10.10.121:80; #ip_hash; #} server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location /back/ { proxy_pass http://192.168.3.236:8080/back/; #後面的斜槓不能少,做用是不日後端傳遞/mail-api 這個路徑 #roxy_redirect off; #proxy_set_header Host mailapi.domain.com; #傳遞不一樣的host給後方節點,實現IP和域名都可以訪問 #proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /common/ { proxy_pass http://192.168.3.236:8088/common/; #proxy_redirect off; #proxy_set_header Host $host; # proxy_set_header Host otherapi1.domain.com; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location = / { #proxy_set_header Host $host; proxy_set_header X-Real-Ip $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://192.168.3.236:8080/back/login; } #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; # } #} }
proxy_pass依然可以使用error_pagehtml
location / { proxy_pass https://10.10.10.244:444; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_intercept_errors on; proxy_redirect default; }
windows下重啓nginxnginx
net stop nginx taskkill /f /t /im nginx.exe net start nginx