nginx反向代理到本地主机

时间:2020-03-16 08:18:58

标签: amazon-web-services nginx reverse-proxy nginx-reverse-proxy

我在我的AWS EC2实例的localhost:8080上运行了应用程序。

为了使用这些Web门户,我在EC2上安装了nginx并反转了代理localhost,以便可以在浏览器上访问Web UI。

Nginx.conf文件

server {
    listen 80;
    server_name ec2-xx-xx-xxx-xxx.eu-central-1.compute.amazonaws.com;

    location / {
    proxy_pass http://localhost:8080;

   location /$request_uri {
   proxy_pass http://localhost$request_uri;

} 

}

在浏览器中单击EC2 URL时,我成功看到了应用程序的主页。

但是当我输入任何URL时,例如/ admin,Nginx重定向到我本地计算机的 localhost:8080 / admin 而不是服务器的localhost。 我想要的是,在命中任何URL时,nginx都应将请求转发到localhost:8080 {$ URL}并返回浏览器。

请提出我错了的地方。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您无需添加第二个位置。

location / {
    proxy_pass http://localhost:8080;
    proxy_set_header   Host $http_host;
    proxy_set_header   X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
}

这应该足以访问您需要的任何内容。