2015-10-20 560 views
12

我有多个ASP.NET应用程序在单个IIS服务器上运行,每个应用程序使用不同的端口。使用nginx作为IIS服务器的反向代理

我已经安装在同一台服务器上的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; 
    } 

然后我重启nginx的,并在浏览器中输入网址http://127.0.0.1/students/。 Nginx提供了404页面。

我没有做任何其他更改的conf文件。

我做错了什么?

+0

该配置看起来完全正确;我们在工作中使用非常类似的配置,在位置块内部使用proxy_pass,而不使用URI部分,从而将请求传递给不同的服务器,且URI不变。 您能否验证Nginx是否正在进入位置块? – ThrawnCA

回答

-2

尝试使用这个指令

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

和第二

+0

Nginx给我错误...上游不允许在这里 – Shuaib

+0

请给你的答案添加一些解释! –

11

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

你需要把该URL的末尾/proxy_pass指令:

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的CONFIGS一个微妙但重要的疑难杂症!如果您不包含反斜杠,http://127.0.0.1:84将被视为服务器位置。如果你确实有反斜杠,它将被视为一个URL,它将代替代理URL中的所有内容直到'位置'部分。

+0

如果我浏览http://127.0.0.1:84/students,我会得到我的网页,因为IIS正在监听84.我想要的是,如果我浏览http://127.0.0.1/students,那么nginx应该重写url到http://127.0.0.1:84。这在nginx中可能吗? – Shuaib

+0

@Shuaib我相信这正是上面的配置发生的情况。 – jwg

+0

我仍然得到一个由nginx提供的404页面。 – Shuaib

0

如果要将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; 
}