【问题标题】:Using nginx as a reverse proxy to IIS server使用 nginx 作为 IIS 服务器的反向代理
【发布时间】:2015-10-20 01:53:05
【问题描述】:

我有多个 ASP.NET 应用程序在单个 IIS 服务器上运行,每个应用程序具有不同的端口。

我在同一台服务器上安装了 nginx,这样我的客户就可以只使用端口 80 访问我的所有应用程序。

Nginx 在 80 端口上运行良好。我的个人 ASP.NET 应用程序也已启动并运行。

我在 nginx 配置文件中进行了这些更改

    location /students/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:84;
    }
    location /registration/ {
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://127.0.0.1:82;
    }

然后我重新启动了 nginx 并在浏览器中输入了 url http://127.0.0.1/students/。 Nginx 提供 404 页面。

我没有对 conf 文件进行其他更改。

我做错了什么?

【问题讨论】:

  • 该配置看起来完全正确;我们在我的工作中有非常相似的配置,使用 proxy_pass,在一个没有 URI 部分的位置块内,将请求传递到 URI 不变的不同服务器。能否验证 Nginx 是否进入了 location 块?

标签: asp.net iis nginx


【解决方案1】:

我相信您遇到的问题与 URL 路径的开头有关。 URL http://120.0.0.1:84/students/ 是返回页面还是 404?如果您期待去http://127.0.0.1:80/students/ 并查看http://127.0.0.1/ 的页面,您会发现nginx 不会使用此配置为您转换路径。相反,它会在代理服务器上查找完全相同的路径。

您需要将/ 放在proxy_pass 指令中的URL 末尾:

location /students/ {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:84/;
}

这是 nginx 配置中一个微妙但重要的问题!如果不包含反斜杠,http://127.0.0.1:84 将被视为服务器位置。如果您确实有反斜杠,它将被视为一个 URL,它将替换代理 URL 中的所有内容,直到“位置”部分。

【讨论】:

  • 如果我浏览 127.0.0.1:84/students 我会得到我的页面,因为 IIS 正在监听 84。我想要的是如果我浏览 127.0.0.1/students 然后 nginx 应该将 url 重写为 127.0.0.1:84。这在 nginx 中可行吗?
  • @Shuaib 我相信这正是上面的配置所发生的。
  • 我仍然得到一个由 nginx 提供的 404 页面。
  • 我要去 127.0.0.1/students。我不想去 127.0.0.1:84/students ... IIS 可以提供服务,我不需要 nginx @jwg。
  • 你要去http://127.0.0.1/students还是http://127.0.0.1/students/?抱歉,StackOverflow 删除了 URL 末尾的斜杠,这就是问题的重点!
【解决方案2】:

如果要将 IIS 127.0.0.1:84/students 转换为 nginx 127.0.0.1/students 。试试下面的代码..

location /students {
   proxy_set_header X-Real-IP  $remote_addr;
   proxy_set_header X-Forwarded-For $remote_addr;
   proxy_set_header Host $host;
   proxy_pass http://127.0.0.1:84/students;
 }
location /registration {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:82/registration;
}

【讨论】:

    【解决方案3】:

    尝试使用该指令

     upstream http://localhost {
         server 127.0.0.1:84;
     }
    

    和第二个相同的块

    【讨论】:

    • Nginx 给我错误...这里不允许上游
    • 请给你的答案添加一些解释!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 2018-09-10
    • 2017-07-17
    • 2020-08-19
    • 2020-06-29
    • 2020-09-22
    相关资源
    最近更新 更多