Nginx

Nginx proxy_pass long 指令

  • October 15, 2019

我想做proxy_pass

https://foo.bar/api/v1/users/{userId}/store/{storeCode}/statushttps://bar.foo/api/v1/users/{userId}/store/{storeCode}/status

並且

https://foo.bar/api/v1/users/{userId}/opendatehttps://bar.foo/api/v1/users/{userId}/opendate

這2個連結的不同之處是用於訪問的http方法,稍後我們將使用limit_except限制訪問,但現在我想先proxy_pass請求。

我做了這樣的配置

location /api/v1/users/[0-9a-z]+/store {
   proxy_pass https://bar.foo/api/v1/users/[0-9a-z]+/store;
   proxy_set_header Host $proxy_host;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
   proxy_redirect off;
   proxy_buffering off;
   proxy_ssl_protocols TLSv1.2;
   proxy_ssl_server_name on;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_pass_request_headers on;
}


location /api/v1/users/[0-9a-z]+/opendate {
   proxy_pass https://bar.foo/api/v1/users/[0-9a-z]+/opendate;
   proxy_set_header Host $proxy_host;
   proxy_set_header X-Forwarded-Proto $scheme;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For 
   proxy_add_x_forwarded_for;
   proxy_redirect off;
   proxy_buffering off;
   proxy_ssl_protocols TLSv1.2;
   proxy_ssl_server_name on;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
   proxy_pass_request_headers on;
}

但我仍然沒有讓我的伺服器代理請求。有沒有人知道如何在 nginx 中使用一些正則表達式來 proxy_pass long 指令位置?

如果那是正則表達式,則說明您使用了錯誤的location指令語法。有關詳細資訊,請參閱此文件

proxy_pass如果在通過上游之前更改了 URI,則只需提供URI 部分。有關詳細資訊,請參閱此文件

例如:

location ~ ^/api/v1/users/[0-9a-z]+/store {
   proxy_pass https://bar.foo;
   ...
}

引用自:https://serverfault.com/questions/988113