1. 程式人生 > 資訊 >B 站過去一年共封禁 8.4 萬個營銷賬號,能量加油站情緒疏導超 6 萬例

B 站過去一年共封禁 8.4 萬個營銷賬號,能量加油站情緒疏導超 6 萬例

使用nginx做反向代理的時候,可以簡單的直接把請求原封不動的轉發給下一個服務。設定proxy_pass請求只會替換域名,如果要根據不同的url字尾來訪問不同的服務,則需要通過如下方法:

方法一:加"/"
server {

listen              8000;
server_name         abc.com;
access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;

location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://user/;
}

location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://order/;
}

}

server {

listen              8000;
server_name         abc.com;
access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;

location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  www.diuxie.com$proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://user/;
}

location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://order/;
}

}
^~/user/表示匹配字首是user的請求,proxy_pass的結尾有/, 則會把/user/*後面的手遊路徑直接拼接到後面,即移除user。

方法二:rewrite
upstream user {
server localhost:8089 weight=5;
}
upstream order {
server localhost:8090 weight=5;
}

server {

listen              80;
server_name  abc.com;
access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;

location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/user/(.*)$ /$1 break;
    proxy_pass http://user;
}

location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/order/(.*)$ /$1 break;
    proxy_pass http://order;
}

}

upstream user {
server localhost:8089 weight=5;
}
upstream order {
server localhost:8090 weight=5;
}

server {

listen              80;
server_name  abc.com;
access_log  "pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G"  main;

location ^~/user/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/user/(.*)$ /$1 break;
    proxy_pass http://user;
}

location ^~/order/ {
    proxy_set_header Host $host;
    proxy_set_header  X-Real-IP        $remote_addr;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-NginX-Proxy true;

    rewrite ^/order/(.*)$ /$1 break;
    proxy_pass http://order;
}

}
proxy_pass結尾沒有/, rewrite重寫了url。