Configuración de Nginx para WordPress dentro del contenedor

3 minutos de lectura

Tengo:

  1. WordPress + MariaDB dentro de contenedores (docker-compose)

  2. Nginx instalado en el sistema (no dentro del contenedor)

Tengo una aplicación web más que se transmite a través de nginx, por lo que no quiero poner nginx dentro del contenedor como recomiendan todas las guías. Solo necesito establecer un proxy o configurarlo para ver el sitio de WordPress desde el contenedor.

¿Cómo configurarlo para mostrar el sitio de WordPress que está dentro del contenedor?

Mi intento:

/etc/nginx/sites-enabled/mi-sitio.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/mi-sitio.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 

Cuando abro my-site.com veo

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-disponible/mysite (no olvide ingresar ln -s a sitios habilitados) Nginx instalado en el sistema

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 instalado en contenedor para proxy 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;
  }
}

¿Ha sido útil esta solución?