nginx反向代理配置去除字首

ckxllf發表於2019-09-06

  使用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

  }

  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

  }

  }

  ^~/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

  }

  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

  }

  }

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


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69945560/viewspace-2656282/,如需轉載,請註明出處,否則將追究法律責任。

相關文章