简体   繁体   English

Nginx保留$ request_uri

[英]Nginx preserve $request_uri

I'm not sure if the behavior I want is actually possible natively with nginx but here goes. 我不确定我想要的行为实际上是否可以通过nginx本地实现,但这已经过去了。

I have a server running on port 81 with the following nginx config: 我有一个使用以下nginx配置在端口81上运行的服务器:

CONFIGURATION OF SERVER1 NGINX
server {
       listen 81;
       server_name SERVER_DNS_NAME;

       location /server1 {
                proxy_pass http://127.0.0.1:8084/;
                proxy_set_header Host $host;
       }

       location / {
                    proxy_pass http://127.0.0.1:8084;
                    proxy_set_header Host $host:$server_port;
                    }

       }

I have another server running on port 82 with similar configuration. 我在具有类似配置的端口82上运行了另一台服务器。 Now what'd i'd like to do is be able to visit them both from port 80 with just different uris. 现在我想做的是能够从80端口以不同的uri访问它们。

For example: URL/server1 would take me to the first server, and URL/server2 would take me to the second. 例如:URL / server1将带我到第一台服务器,URL / server2将带我到第二台服务器。

CONFIGURATION OF NGINX LISTENING ON PORT 80
server {
       listen SERVER_IP:80;
       location /server1{
                    proxy_set_header Host $host;
                    http://SERVER_IP:81;


                    }
        location /server2 {
                 proxy_pass http://SERVER_IP:82;
                 proxy_set_header Host $host;
        }

This works fine when I go to URL/server1. 当我转到URL / server1时,这工作正常。 I am successfully routed to the main page on server1. 我已成功路由到server1上的主页。 However as soon as I click any of the links present on the page on server1 I get a 404. This is because the site tries to go to URL/some_subdir_of_server1 (for which there is no mapping) rather than doing URL/server1/some_subdir_of_server1. 但是,一旦我单击server1页面上存在的任何链接,我就会得到一个404。这是因为该站点尝试转到URL / some_subdir_of_server1(没有映射),而不是执行URL / server1 / some_subdir_of_server1。 Is this behavior doable? 这种行为可行吗? If so how? 如果可以,怎么办?

Thanks! 谢谢!

Be careful with trailing slashes: in your example, you have proxy_pass http://SERVER_IP:81/; 在斜杠后面要小心:在您的示例中,您具有proxy_pass http://SERVER_IP:81/; which will set the proxy URL to root / 这会将代理URL设置为root /

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

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