我知道这是一个常见的问题,并且有相同的答案,但我问这个问题的原因是因为我不知道如何处理解决方案。根据我决定的方式,我可以选择改变的解决方案。反正,
我有一个AWS EC2实例。我的DNS由Route53处理,我拥有example.com。目前,在我的实例上,有两个服务正在运行:
example.com:80 [nginx/php/wordpress]
example.com:8142 [flask]
我想要做的是,app.example.com
指向example.com:8142
。我该怎么做呢?我非常确定我必须将app.example.com
指向与example.com
相同的IP,因为它与提供它的盒子相同。并且,nginx将是第一个在端口80处理这些请求的人。有没有办法让nginx将所有请求转发到localhost:8142?
有没有更好的方法可以解决这个问题?
答案 0 :(得分:65)
您可以为app.example.com添加一个虚拟主机,该虚拟主机侦听端口80,然后代理将所有请求传递给flask:
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://localhost:8142;
}
}
答案 1 :(得分:1)
这就是您要用apache做的方式。
$cat /etc/apache2/sites-available/app.conf
<VirtualHost *:80>
ServerName app.example.com
ProxyPreserveHost On
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8142/
ProxyPassReverse / http://localhost:8142/
</VirtualHost>
答案 2 :(得分:0)
您可以将域重定向到某个端口。这取决于您使用的-Nginx / Apache的Web服务。如果您使用的是Nginx,则需要在Nginx的网站配置中添加服务器块。这可以通过使用波纹管
来实现location /{
proxy_pass http://127.0.0.1:8142/;
}
如果您使用的是Apache,则有两种选择,第一种是在网站的.htaccess中添加重定向规则,第二种是直接在Apache的Vhost文件中进行重定向。我喜欢使用第一个选项。在您的.htaccess文件中,您可以添加以下规则
RewriteEngine on
# redirect to 3000 if current port is not 3000 and "some-prefix/" is matched
RewriteRule ^/(.*[^/])/?$ http://blabla:3000/$1/ [R=301,L]
如果您想使用Apache的Vhost文件,建议您阅读以下教程link
答案 3 :(得分:0)
我有带有两个 NodeJS 实例的 ubuntu 16 和 nginx,一个用于前端,一个用于管理员。 在,我有: /etc/nginx/sites-available/default
我已添加:
服务器{ ... 地点 / { proxy_pass http://127.0.0.1:8001; }
location /admin {
rewrite ^/admin(.*) /$1 break;
proxy_pass http://127.0.0.1:8002;
}
location /other {
rewrite ^/other(.*) /$1 break;
proxy_pass http://127.0.0.1:8003;
}
...
}
我用它来获得管理员权限。