使用Nginx服务容器化的wordpress

时间:2018-10-05 13:37:12

标签: wordpress docker nginx

我有一台安装了nginx的服务器和多个容器,我希望nginx将请求定向到相应的容器。
容器之一是暴露端口8000的Wordpress容器:

version: '3.3'

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: **
       MYSQL_DATABASE: wordpress
       MYSQL_USER: **
       MYSQL_PASSWORD: **

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - "8000:80"
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: **
       WORDPRESS_DB_PASSWORD: **
volumes:
    db_data:

我已成功输入地址http://my_wordpress.com:8000中的Wordpress实例。
我应该如何配置nginx,以便在我请求http://my_wordpress.com时将请求转发到WordPress?
我当前的wordpress.conf

server {
        listen 80;
        server_name my_wordpress.com;
        return 301 https://my_wordpress.com$request_uri;
}
server {
        listen 443 ssl spdy; 
        server_name my_wordpress.com;
        ssl_certificate ***********.pem;
        ssl_certificate_key ***************.pem;
        ssl_protocols ***************;
        ssl_ciphers '*******************';
        ssl_prefer_server_ciphers on;
        location / {
            try_files $uri $uri/ /index.php?$args;
        }

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass my_wordpress.com:8000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }
}

3 个答案:

答案 0 :(得分:1)

您可以使用nginx-proxy来自动创建要公开的容器的反向代理。使用起来非常简单快捷。

例如,您的部署将是:

version: '3.3'

services:
   nginx-proxy:
     image: jwilder/nginx-proxy
     ports:
       - "80:80"
     volumes:
       - /var/run/docker.sock:/tmp/docker.sock:ro
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: **
       MYSQL_DATABASE: wordpress
       MYSQL_USER: **
       MYSQL_PASSWORD: **

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: **
       WORDPRESS_DB_PASSWORD: **
       VIRTUAL_HOST=my_wordpress.local
volumes:
    db_data:

答案 1 :(得分:0)

您可以这样做

version: '3.3'

services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: **
      MYSQL_DATABASE: wordpress
      MYSQL_USER: **
      MYSQL_PASSWORD: **
    networks:
      - my_network

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: **
      WORDPRESS_DB_PASSWORD: **
    networks:
      - my_network

  nginx:
    image: nginx_revpxy    
    build:
      context: .
      dockerfile: DockerfileForNginx
    networks:
      - my_network

network:
  my_network:
    driver: bridge

# DockerfileForNginx file
# 
# FROM nginx:alpine
# RUN rm /etc/nginx/nginx.conf
# COPY nginx.conf /etc/nginx/


# Example of nginx.conf
# server {
#         listen 80;
#         server_name wordpress;
#         return 301 https://my_wordpress.com$request_uri;
# }

如果您设置了网络,则可以通过服务名称与容器通信。只需确保将正确的nginx.conf加载到Docker容器中,并且您有一个反向代理容器即可与同一个docker网络中的其他容器进行通信。

答案 2 :(得分:0)

解决方案是将Wordpress设置中的主机名配置设置为:https://my_wordpress.com

最终,事实证明Wordpress通过配置声明其主机名,默认配置(在Wordpress安装后)为:http://my_wordpress.com:8000,因此Wordpress从https://my_wordpress.com重定向到http://my_wordpress.com:8000,很多麻烦。

因为我是Nginx,docker和Wordpress的新手,所以我感到困惑并认为Wordpress会根据从服务器获得的主机名来获取主机名,而问题出在服务器配置级别。

作为参考,这是有效的wordpress.conf

server {
        listen 80;
        server_name my_wordpress.com;
        return 301 https://my_wordpress.com$request_uri;
}
server {
        listen 443 ssl http2;
        server_name my_wordpress.com;
        ssl_certificate ***************.pem;
        ssl_certificate_key **************.pem;
        ssl_protocols ************;
        ssl_ciphers '*********';
        ssl_prefer_server_ciphers on;
        location / {
                proxy_pass http://localhost:8000;
                proxy_set_header HOST $host;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}