【问题标题】:Nginx configuration to Wordpress inside containerNginx 配置到容器内的 Wordpress
【发布时间】:2020-08-21 13:55:37
【问题描述】:

我有:

  1. 容器内的 Wordpress + MariaDB (docker-compose)

  2. Nginx 安装到系统中(不在容器内)

我还有一个通过 nginx 代理的 web 应用程序,所以我不想像所有指南推荐的那样将 nginx 放入容器中。我只需要设置代理或配置它以从容器中查看 Wordpress 站点。

如何配置它以显示容器内的 Wordpress 网站?

我的尝试:

/etc/nginx/sites-enabled/my-site.com

server {
 listen 80;
 listen [::]:80;

 server_name my-site.com;
 
 root /root/my-site.com/wordpress; 
 index index.php;
 
 location / {
     try_files $uri $uri/ /index.php$is_args$args;
 }

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

 location ~ /.ht {
     deny all;
 }

 location = /favicon.ico { 
     log_not_found off;
     access_log off; 
 }

 location = /robots.txt { 
     log_not_found off;
     access_log off;
     allow all; 
 }

 location ~* .(css|gif|ico|jpeg|jpg|js|png)$ {
     expires max;
     log_not_found off;
 }

}

/root/my-site.com/docker-compose.yml

    version: '3'

services:

  wordpress:
    image: wordpress:5.5.0-fpm-alpine
    links:
      - mariadb:mysql
    depends_on:
      - mariadb
    env_file: .env
    environment:
      - WORDPRESS_DB_HOST=mariadb:3306
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=my-site_database
    ports:
      - '9000:9000'
    volumes:
      - ./wordpress:/var/www/html
    networks:
      - my-site-network
           
  mariadb:
    image: mariadb
    env_file: .env
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD
      - MYSQL_DATABASE=my-site_database
    ports:
      - '3306:3306'
    volumes:
      - ./database:/var/lib/mysql
    networks:
      - my-site-network

networks:
  my-site-network:
    driver: bridge 

当我打开 my-site.com 时,我看到了

404 Not Found
nginx/1.14.0 (Ubuntu)

【问题讨论】:

    标签: wordpress docker docker-compose containers


    【解决方案1】:

    docker-compose.yml

    version: '3'
    services:
      mysql:
        image: mariadb
        volumes:
          - ./data/mysql:/var/lib/mysql
        environment:
          - MYSQL_ROOT_PASSWORD=myrootpassword
          - MYSQL_DATABASE=mydatabase
          - MYSQL_USER=myusername
          - MYSQL_PASSWORD=mypassword
        restart: always
      wordpress:
        image: wordpress:php7.3-fpm-alpine
        volumes:
          - ./data/html:/var/www/html
        depends_on:
          - mysql
        environment:
          - WORDPRESS_DB_HOST=mysql
          - MYSQL_ROOT_PASSWORD=myrootpassword
          - WORDPRESS_DB_NAME=mydatabase
          - WORDPRESS_DB_USER=mydbuser
          - WORDPRESS_DB_PASSWORD=mypassword
          - WORDPRESS_TABLE_PREFIX=wp_
        links:
          - mysql
        restart: always
      phpmyadmin:
        depends_on:
          - mysql
        image: phpmyadmin/phpmyadmin
        restart: always
        ports:
          - '8181:80'
        environment:
          - PMA_HOST=mysql
          - MYSQL_ROOT_PASSWORD=myrootpassword
      nginx:
        image: nginx:alpine
        volumes:
          - ./nginx:/etc/nginx/conf.d
          - ./data/html:/var/www/html
        ports:
          - 8080:80
        links:
          - wordpress
    

    /etc/nginx/sites-available/mysite (别忘了 ln -s to sites-enabled) Nginx 安装在系统中

    server {
      listen 80;
      listen [::]:80;
      listen 443 ssl;
      ssl_certificate /etc/nginx/ssl_certs/mysite.crt;
      ssl_certificate_key /etc/nginx/ssl_certs/mysite.key;
      client_max_body_size 10m;
    
      server_name mysite.com www.mysite.com;
    
      location / {
            proxy_intercept_errors  on;
            proxy_connect_timeout 180;
            proxy_send_timeout    180;
            proxy_read_timeout    180;
            proxy_pass              http://localhost:8080/; #nginx container
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto https;
      }
    }
    

    ./nginx/nginx.conf Nginx 安装在容器中以代理 wordpress

    server {
      listen 80;
      listen [::]:80;
      access_log off;
      root /var/www/html;
    
      index index.php;
      server_tokens off;
      location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ /index.php?$args;
      }
      # pass the PHP scripts to FastCGI server listening on wordpress:9000
      location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass wordpress:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 2021-08-14
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2015-12-31
      • 2016-11-03
      相关资源
      最近更新 更多