使用ProxyPassconfigurationNginx过滤的正确方法

我使用Nginx来代理传递给我的wsgi应用程序与马戏团服务。

我想允许通过该应用程序的stream量只为一些IP地址的一些应用程序的url。

现在看起来像这样:

server { listen 80; server_name service.dev; access_log /var/log/service.access.log; error_log /var/log/service.error.log debug; location / { try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://127.0.0.1:9000/; } location /admin/ { allow 192.168.5.0/24; deny all; } } 

但它不起作用。 如果我有权利,我得到一个404错误,而不是proxy_pass。

你知道我怎么做,而不必每次都复制/粘贴proxy_passconfiguration?

谢谢。

编辑2

根据VBart的评论,我已经将try_files的条目更改为$uri @proxy_to_app 。 这避免了对指定位置的顺序的任何混淆(它们必须始终保持最后)。 请注意,如果/admin/目录在本地存在,则将使用此代替代理。

编辑

如果您确实想使用指定位置来避免为每个位置重复使用proxy_pass ,则可以使用以下命令:

 server { listen 80; server_name service.dev; access_log /var/log/service.access.log; error_log /var/log/service.error.log debug; location / { # Catch all try_files $uri @proxy_to_app; } location /admin/ { # /admin/ only allow 192.168.5.0/24; deny all; try_files $uri @proxy_to_app; } location @proxy_to_app { proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://127.0.0.1:9000; } } 

这是一个黑客攻击, try_files至less需要两个参数,在第一个实例中,它将使用相同的$uri查找本地path(如果要用本地文件覆盖)。 在第二个例子中,我将/dev/null指定为第二个path; 这将永远不会被使用。

原版的

试试这个configuration:

 server { listen 80; server_name service.dev; access_log /var/log/service.access.log; error_log /var/log/service.error.log debug; # Proxy settings proxy_set_header Host $http_host; proxy_redirect off; location / { # Catch all proxy_pass http://127.0.0.1:9000/; } location /admin/ { # /admin/ only allow 192.168.5.0/24; deny all; proxy_pass http://127.0.0.1:9000/; } } 

location /块应该只捕获在其他位置块中未被匹配的URI。