【问题标题】:Let Nginx pass current location让 Nginx 通过当前位置
【发布时间】:2018-10-05 14:26:31
【问题描述】:

我在 IIS 上对我的站点有 nginx 反向代理,这是我的 nginx 配置:

更新

upstream backend  {
  server 43.128.77.101;
}

server {
  server_name domain.subdomain.com;   

  location /products {
    if ($query_string ~ Jeans){
      return 301 /get-all-products/?filter=jeans;
    }
    if ($query_string ~ Shirts){
      return 301 /get-all-products/?filter=shirts;
    }
    if ($query_string ~ Hats){
      return 301 /get-all-products/?filter=hats;
    }
  }

  location / {  
    proxy_pass  http://backend;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }   
}

它通过查询字符串从/products 页面重定向到某些URL。但是对于页面 /products-available 它失败并出现错误 404。Nginx 错误日志包含错误:

“/usr/share/nginx/html/products-available”失败(2:没有这样的文件或目录)

页面/products-available 不需要任何重定向。我希望它按原样传递给后端 IIS 服务器。我怎样才能告诉nginx通过它?我做错了什么?

谢谢。

【问题讨论】:

    标签: nginx nginx-location nginx-reverse-proxy


    【解决方案1】:

    这是因为您只是为给定路径 (/products) 定义 Nginx 的行为。

    如果您想为与 /products 路径不匹配的 Nginx 请求定义默认行为(例如 /products-available),您可以在当前位置部分之后添加以下内容,以将任何其他路径请求代理到不同的应用程序/端口。

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
    

    您可以在https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/#passing-a-request-to-a-proxied-server 中查看有关向其他应用程序发送请求的更多信息

    【讨论】:

    • 请考虑更新的问题。我已经添加了完整的配置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 2013-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 1970-01-01
    相关资源
    最近更新 更多