原始的 nginx 配置php
upstream service_test {css
server 127.0.0.1:8080;html
}nginx
serveride
{svg
listen 80;server
server_name test.com;htm
index index.html index.php;rem
root /tmp/test.com;it
error_page 404 http://test.com/404.html;
error_page 502 http://test.com/502.html;
error_page 500 http://test.com/500.html;
location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
{
rewrite ^(.*)$ /static$1 break;
root /tmp/test.com; #
expires 1d;
}
location ~* \.(html|htm)$
{
rewrite ^(.*)$ /static$1 break;
roo /tmp/test.com; #
expires 900s;
}
location / {
proxy_pass http://service_test;
include /opt/conf/nginx/proxy.conf;
}
修改後的 Nginx 配置
upstream service_test {
server 127.0.0.1:8080;
}
server
{
listen 80;
server_name test.com;
index index.html index.php;
root /tmp/test.com;
error_page 404 http://test.com/404.html;
error_page 502 http://test.com/502.html;
error_page 500 http://test.com/500.html;
location ~* \.(gif|jpg|jpeg|png|css|js|ico|txt|svg|woff|ttf|eot)$
{
rewrite ^(.*)$ /static$1 break;
root /tmp/test.com; #
expires 1d;
}
location ~* \.(html|htm)$
{
rewrite ^(.*)$ /static$1 break;
roo /tmp/test.com; #
expires 900s;
}
set $flag 0;
if ($request_uri ~* "^/fuck/\w+\.html$") {
set $flag "${flag}1";
}
if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
set $flag "${flag}2";
}
if ($flag = "012") {
rewrite ^ /index.html permanent;
}
location / {
proxy_pass http://service_test;
include /opt/conf/nginx/proxy.conf;
}
在實現需求的過程當中出現的問題
把 if 指令 和 proxy_pass 都放在 location 下面的話,if 指令裏面的內容不會執行,只會執行 proxy_pass。
location / { if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") { rewrite ^ /index.html permanent; } proxy_pass http://service_test; include /opt/conf/nginx/proxy.conf;}
if 指令下面使用 proxy_pass 指令問題
像下面這樣使用會報錯,錯誤的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com/fuck;
}
正確的方式:
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com$request_uri;
}
或是
if ($remote_addr ~* "192.168.0.50|192.168.0.51|192.168.0.56") {
proxy_pass http://test.com;
}
若是你是直接另外啓動一個 location 的話,好比啓動以下 location :
location /fund { if ($remote_addr !~* "192.168.0.50|192.168.0.51|192.168.0.56") {
rewrite ^ /index.html permanent;
} }
這樣的方式也是不支持的,當用 IP 192.168.0.50 訪問的時候,沒有達到咱們的業務需求,會報錯 400