【问题标题】:Nginx Reverse Proxy on Docker - How to connect to other application?Docker 上的 Nginx 反向代理 - 如何连接到其他应用程序?
【发布时间】:2021-11-21 20:25:20
【问题描述】:

我使用 docker compose 在我的 NAS 上的 docker 容器中运行 Joplin。现在我想设置一个反向代理,以便通过我的个人域访问它。

joplin/docker-compose.yml 文件如下所示:

version: '3'

services:
    db:
        image: postgres:13.1
        volumes:
            - /local/joplin:/var/lib/postgresql/data
        restart: unless-stopped
        environment:
            - APP_PORT=22300
            - POSTGRES_PASSWORD=********
            - POSTGRES_USER=user
            - POSTGRES_DB=database
    app:
        image: joplin/server:2.2.10
        depends_on:
            - db
        ports:
            - "22300:22300"
        restart: unless-stopped
        environment:
            - APP_PORT=22300
            - APP_BASE_URL=http://192.168.1.2:22300/
            - DB_CLIENT=pg
            - POSTGRES_PASSWORD=********
            - POSTGRES_DATABASE=database
            - POSTGRES_USER=user
            - POSTGRES_PORT=5432
            - POSTGRES_HOST=db

nginx/docker-compose.yml 文件如下所示:

version: '3'

services:
    nginx: 
        image: nginx:latest
        ports:
            - 8080:80
        volumes:
            - /local/nginx/nginx.conf:/etc/nginx/nginx.conf
            - /local/nginx/sites-enabled:/etc/nginx/sites-enabled

我为我的/local/nginx/nginx.conf 使用了默认值。如下。 :

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

此外,在/local/nginx/sites-enabled/ 文件夹中,我创建了以下文件:

  • /local/nginx/sites-enabled/example.org,
  • /local/nginx/sites-enabled/my.example.org

/local/nginx/sites-enabled/example.org的内容是:

##
# example.org -- Configuration
server {
        listen 80;
        listen [::]:80;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name example.org;
}

/local/nginx/sites-enabled/my.example.org的内容是:

##
# my.example.org -- Configuration
server {
    listen 80;
    listen [::]:80;
    server_name my.example.org;

    location / {
        proxy_pass http://192.168.1.2:22300/;
        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-Proto $scheme;
        proxy_set_header X-Forwarded-Protocol $scheme;
        proxy_set_header X-Forwarded-Host $http_host;
    }
}

我在路由器上设置了到 nginx 容器的端口转发,它可以工作。 (当我转到http://example.org 时看到 nginx 的 404 屏幕)但是,我很难为 joplin 容器设置反向代理。当我尝试访问 http://my.example.org 时,我收到 510 错误消息。我做错了什么?

奇怪的是,当我将http://192.168.1.2:22300/ 替换为运行测试网页的个人电脑的IP 时,我可以通过http://my.example.org 访问它。即使我在我的电脑上设置了 Joplin,它也可以工作。我的 nginx 或 docker 设置似乎有问题。

【问题讨论】:

    标签: docker nginx docker-compose reverse-proxy nginx-reverse-proxy


    【解决方案1】:

    经过大量调试和谷歌搜索,我终于找到了解决方案。需要做的是:

    1. nginx/docker-compose.yml 内部使用网络设置 nginx:
    version: '3'
    
    services:
        nginx: 
            image: nginx:latest
            ports:
                - 8080:80
            volumes:
                - /local/nginx/nginx.conf:/etc/nginx/nginx.conf
                - /local/nginx/sites-enabled:/etc/nginx/sites-enabled
    #----------------------------------------
    # Setup network my_net
    #----------------------------------------
            networks:
                - my_net
    networks:
        my_net:
            driver: bridge
    
    1. 让 Joplin 使用为 nginx 定义的网络,并添加一个额外的网络用于与数据库通信。 (另外,为 Joplin 容器设置一个名称。)
    version: '3'
    
    services:
        db:
            image: postgres:13.1
            #----------------------------------------
            # Setup communication to Joplin server
            #----------------------------------------
            container_name: database
            networks:
                - joplin_net
            #----------------------------------------
            volumes:
                - /local/joplin:/var/lib/postgresql/data
            restart: unless-stopped
            environment:
                - APP_PORT=22300
                - POSTGRES_PASSWORD=********
                - POSTGRES_USER=user
                - POSTGRES_DB=database
        app:
            image: joplin/server:2.2.10
            #----------------------------------------
            # Setup communication to Postgres server
            # and nginx
            #----------------------------------------
            container_name: joplin # This will be the name used by nginx.
            networks:
                - joplin_net
                - nginx_my_net
            #----------------------------------------
            depends_on:
                - db
            ports:
                - "22300:22300"
            restart: unless-stopped
            environment:
                - APP_PORT=22300
                - APP_BASE_URL=http://192.168.1.2:22300/
                - DB_CLIENT=pg
                - POSTGRES_PASSWORD=********
                - POSTGRES_DATABASE=database
                - POSTGRES_USER=user
                - POSTGRES_PORT=5432
                - POSTGRES_HOST=db
    #----------------------------------------
    # You can replace joplin_net with any
    # name you like. However, the name for
    # nginx_my_net has to be:
    #     app folder + '_' + network name
    # The nginx application is in the nginx
    # folder, therefore the prefix has to be
    # 'nginx_'. The network name is 'my_net',
    # so this has to be the suffix.
    #----------------------------------------
    networks:
        joplin_net:
            driver: bridge
        nginx_my_net:
            external: true
    
    1. /local/nginx/sites-enabled/my.example.org 文件必须修改:
    ##
    # my.example.org -- Configuration
    server {
        listen 80;
        listen [::]:80;
        server_name my.example.org;
        # The next line make nginx use the docker DNS
        # to find the Joplin container by its name
        # ('joplin').
        resolver 127.0.0.11 valid=30;
    
        location / {
            # The server name used here has to be the
            # one defined using 'container_name' in the
            # docker-compose.yml for the application we
            # want to proxy to.
            proxy_pass http://joplin:22300/;
            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-Proto $scheme;
            proxy_set_header X-Forwarded-Protocol $scheme;
            proxy_set_header X-Forwarded-Host $http_host;
        }
    }
    

    希望这可以为某人节省几天的时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-20
      • 2019-07-30
      • 1970-01-01
      • 2019-09-27
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-15
      相关资源
      最近更新 更多