简体   繁体   English

Nginx代理到本地主机上的应用程序

[英]Nginx proxy to application on localhost

I have an nginx webserver set up on port 80. My (SonarQube) app runs on port 9000. When I request http://www.example.com/sonar/ I wish to be redirected to Sonar, so i added this to my nginx config: 我在端口80上设置了Nginx Web服务器。我的(SonarQube)应用程序在端口9000上运行。当我请求http://www.example.com/sonar/时,我希望重定向到Sonar,因此我将其添加到了我的Nginx的配置:

location /sonar/ {
    proxy_pass  http://127.0.0.1:9000/;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_read_timeout  1800;
    proxy_connect_timeout 1800;
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

However, when i go to http://www.example.com/sonar/ , every link on this page goes to http://www.example.com/ instead of http://www.example.com/sonar/ . 但是,当我转到http://www.example.com/sonar/时 ,此页面上的每个链接都转到http://www.example.com/而不是http://www.example.com/sonar/ So stylesheets, css scripts and links are broken. 因此,样式表,css脚本和链接已损坏。 How can i fix this? 我怎样才能解决这个问题? I tried rewrite , but I can't seem to get it working.... 我尝试rewrite ,但似乎无法正常工作。

The problem is in the proxy_pass line. 问题出在proxy_pass行中。 In your case, it should be: 在您的情况下,应为:

proxy_pass  http://127.0.0.1:9000;

But NOT: 但不是:

proxy_pass  http://127.0.0.1:9000/;

In proxy_pass directive, if you specify the URI which is / in your case, the matched URI /sonar/ will be replaced as the specified one / . proxy_pass指令中,如果您指定的URI是/ ,则匹配的URI /sonar/将替换为指定的/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM