Nginx configuration for WordPress fpm-alpine

Viewed 470

I'm trying to optimize my server Nginx performance, but I cannot figure out how. my server is ubuntu 20.04, and I have an Nginx installed locally and use it to reverse proxy to my 2 websites.

my native Nginx config for redirect to website1 is like this:

server {
    listen 80; listen [::]:80;
    server_name website1.com www.website1.com;

    location / {
        client_max_body_size 5M;     # --> Maximum 5MB file is allowed to upload
        proxy_pass http://127.0.0.1:9191/;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
 }
}

and website1 is set up with the following docker-compose file:


version: "3.7"

services:

  # Database for wordpress mysql
  mysql_db:
    image: mysql:8.0
    container_name: mysql
    restart: unless-stopped
    env_file: .env
    environment:
      - MYSQL_DATABASE=db_name
    volumes:
      - db:/var/lib/mysql
    command: '--default-authentication-plugin=mysql_native_password'
    networks:
      - ntk

  # Wordpress service
  wordpress:
    depends_on:
      - mysql_db
      - redis
    image: wordpress:fpm-alpine
    container_name: wordpress
    restart: unless-stopped
    env_file: .env
    environment:
      - WORDPRESS_DB_HOST=mysql_db:3308
      - WORDPRESS_DB_USER=$MYSQL_USER
      - WORDPRESS_DB_PASSWORD=$MYSQL_PASSWORD
      - WORDPRESS_DB_NAME=stsecret_db_name
      - WORDPRESS_CONFIG_EXTRA=
           define('WP_REDIS_HOST','redis');
           define('WP_REDIS_PORT','9649');
           define('WP_CACHE_KEY_SALT','password');
           define('WP_CACHE','true');

    volumes:
      - wp:/var/www/html
    networks:
      - ntk
      
  # Nginx webserver
  nginx:
    depends_on:
      - wordpress
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    ports:
      - 9191:80
    volumes:
      - ./nginx-conf:/etc/nginx/conf.d
      - wp:/var/www/html

    networks:
      - ntk
           
  # Redis 
  redis:
    image: redis:alpine
    container_name: redis
    restart: unless-stopped
    ports:
      - 9649:9649
      
    volumes:
      - rd:/var/www/html
             
    networks:
      - ntk          
    entrypoint: redis-server --maxmemory 256mb --maxmemory-policy allkeys-lru
  
volumes:
  db:
  wp:
  rd:
networks:
  ntk:
    driver: bridge


and the docker Nginx configuration for website1 that is passed on:

server {
    listen 80;
    listen [::]:80;
    server_name server_name website1.ir www.website1.ir;

    # Gzip configuration
    gzip on;
    gzip_static on; # allows pre-serving of .gz file if it exists
    gzip_disable "msie6"; # Disable for user-agent Internet explorer 6. Not supported.
    gzip_proxied any; # enable gzip for all proxied requests
    gzip_buffers 16 8k; # number and size of buffers to compress a response
    gzip_http_version 1.1;
    gzip_min_length 256; # Only gzip files of size in bytes
    gzip_types text/plain text/css text/html application/javascript application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript $
    gunzip on; # Uncompress on the fly

    # Wordpress
    location / {
        client_max_body_size 20M;
        index index.php index.html index.htm;
        root /var/www/html;
        try_files $uri $uri/ /index.php$is_args$args;
    }
    location ~ \.php$ {
        client_max_body_size 20M;
        index index.php index.html index.htm;
        root /var/www/html;
        try_files $uri =404;
        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 PATH_INFO $fastcgi_path_info;
        fastcgi_param PHP_VALUE "upload_max_filesize=128M \n post_max_size=128M";
    }
}

I want to modify my Nginx according to this tutorial, but I have 2 questions that I cannot find the answer to.

  1. even though everything works right now, but isn't using 2 Nginx redundant? I mean, can I use the locally installed Nginx and get rid of the docker Nginx? and if yes, how? I tried to run docker-compose without the Nginx and exposing WordPress 9191 port to outside, so Nginx could directly redirect to it, but it didn't work!

  2. if using 2 Nginx is mandatory, where should I add the things that are mentioned in this tutorial, like Micro caching, Pagespeed Module and rate limit? should it be added to the local Nginx or the one inside the docker ?

Thanks.

0 Answers
Related