Nginx配置到容器内的Wordpress

问题描述

我有

  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)

解决方法

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改为启用站点)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;
  }
}