简体   繁体   English

带有Docker容器的Nginx代理

[英]Nginx proxy with Docker containers

This is my docker infra. 这是我的码头工人基础设施。

nginx webserver serving on port 80 (exposed to host) 在端口80上服务的nginx Web服务器(公开给主机)

media and blog serving on port 80 (not exposed to host) running under individual container 在端口80(不暴露给主机)上运行的媒体和博客在单独的容器下运行

All running on the same VM. 所有运行在同一虚拟机上。


        Name                      Command               State         Ports
----------------------------------------------------------------------------------
media                  docker-php-entrypoint apac ...   Up      80/tcp
mysql_db_blog          docker-entrypoint.sh mysqld      Up      3306/tcp
mysql_db_media         docker-entrypoint.sh mysqld      Up      3306/tcp
webserver              nginx -g daemon off;             Up      0.0.0.0:80->80/tcp
blog                   docker-entrypoint.sh apach ...   Up      80/tcp

My nginx config: 我的nginx配置:

server {
        listen 80 default_server;
        server_name 192.168.0.7;
        server_tokens off;

        location /story/ {
            proxy_pass         http://blog/;
            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_set_header   X-Forwarded-Host $server_name;
        }

        location /videos/ {
            proxy_pass         http://media/;
            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_set_header   X-Forwarded-Host $server_name;
        }
}

My issue: 我的问题:

When I try to access any of the below URLs 当我尝试访问以下任何URL时

1) http://webserver/story 1) http:// webserver / story

2) http://webserver/videos 2) http:// webserver / videos

after the first request, the last part 'story' or 'videos' gets deleted form the url. 在第一个请求之后,最后一部分“故事”或“视频”将从网址中删除。

What am I missing from nginx conf to make sure 'story' or 'videos' doesn't gets removed from the url? 为了确保不会从URL中删除“故事”或“视频”,nginx conf缺少什么?

It's to do with how proxy_pass is written in your stanza. 这与在节中编写proxy_pass方式有关。

From proxy_pass : proxy_pass

A request URI is passed to the server as follows: 请求URI如下传递给服务器:

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive: 如果使用URI指定了proxy_pass指令,那么当请求传递到服务器时,与该位置匹配的规范化请求URI的一部分将被该指令中指定的URI代替:

location /name/ {
    proxy_pass http://127.0.0.1/remote/;
}

If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI: 如果指定proxy_pass时不带URI,则将请求URI以与客户端在处理原始请求时发送的格式相同的形式传递给服务器,或者在处理更改的URI时传递完整的规范化请求URI:

location /some/path/ {
    proxy_pass http://127.0.0.1;
}

So change your proxy_pass: 因此,更改您的proxy_pass:

location /videos/ {
    proxy_pass         http://media;

Note - lack of trailing slash, means it preserves the original URI. 注意-缺少尾部斜杠,表示它保留了原始URI。

You might also want to look at rewrite as that does something related. 您可能还想看看rewrite因为它做了一些相关的事情。

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

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