使用nginx作为IIS服务器的反向代理

Shu*_*aib 13 asp.net iis nginx

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

我在同一台服务器上安装了nginx,以便我的客户端只能使用端口80访问我的所有应用程序.

Nginx在端口80上运行正常.我的各个ASP.NET应用程序也已启动并运行.

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

    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;
    }
Run Code Online (Sandbox Code Playgroud)

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

我没有对conf文件做任何其他更改.

我做错了什么?

jwg*_*jwg 13

我相信您遇到的问题与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/;
}
Run Code Online (Sandbox Code Playgroud)

这是nginx配置中的一个微妙但重要的陷阱!如果不包含反斜杠,http://127.0.0.1:84则将其视为服务器位置.如果你有反斜杠,它将被视为一个URL,它将替换代理URL中的所有内容,直到'location'部分.


小智 -4

尝试使用该指令

 upstream http://localhost {
     server 127.0.0.1:84;
 }
Run Code Online (Sandbox Code Playgroud)

和第二个相同的块