如何配置使用 WPML 'different domain per language' 的 dockerized wordpress nginx?

How to config dockerized wordpress nginx that using 'different domain per language' of WPML?

现在这是 cool.XXXXXX.com 正在使用的域。

我想展示我的日语版本,域名为 jp-cool.XXXXXX.com

我用 letencrypt 设置了 SSL

certbot certonly --standalone -d jp-cool.XXXXXX.com --staple-ocsp -m root@jp-cool.XXXXXX.com --agree-tos

docker-compose.yml

version: "3.3"
services:
  XXXXweb-db:
    image: mysql:5.7.26
    restart: always
    container_name: XXXXweb-db
    environment:
      MYSQL_HOST: XXXXweb-db
      MYSQL_DATABASE: ${DB_NAME}
      MYSQL_USER: ${DB_USER}
      MYSQL_PASSWORD: ${DB_PASS}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASS}
    volumes:
      - ./data:/var/lib/mysql:delegated
      - ./logs/mysql:/var/log/mysql:delegated
      - ./conf/mysql.cnf:/etc/mysql/my.cnf:delegated
    ports:
        - "3306:3306"
    expose:
        - 3306
    security_opt:
      - seccomp:unconfined
  
  XXXXweb-nginx:
    image: nginx:1.17.1-alpine
    restart: always
    ports:
      - "80:80"
      - "443:443"
    expose:
      - 80
      - 443
    volumes:
      - ./logs:/var/log/nginx:delegated
      - ./conf/${NGINX_CONFIG_NAME}:/etc/nginx/nginx.conf:delegated
      - ${CERT_PATH}:/etc/letsencrypt:delegated
      - ./:/wwwroot:delegated
    depends_on:
      - XXXXweb-db
      - XXXXweb-php
    logging:
        driver: "json-file"
        options:
          max-size: "100m"

  XXXXweb-php:
    image: php-XXXX
    restart: always
    ports:
      - "9000:9000"
    expose:
      - 9000
    volumes:
      - ./logs:/var/log:delegated
      - ./:/wwwroot:delegated
    healthcheck:
      test: ["CMD-SHELL", "pidof php-fpm"]
      interval: 5s
      retries: 12
    logging:
      driver: "json-file"
      options:
        max-size: "100m"

nginx-server.conf

user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events { worker_connections 1024; }

http {
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
        access_log on;
        sendfile on;
        keepalive_timeout 65;
        client_max_body_size 100M;

        server {
            listen 80;
            server_name cool.XXXXXX.com;
            return 301 https://cool.XXXXXX.com$request_uri;
        }

        server {
                listen [::]:443 ssl ipv6only=on; # managed by Certbot
                listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/cool.XXXXXX.com/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/cool.XXXXXX.com/privkey.pem; # managed by Certbot
                
                ## Your website name goes here.
                server_name cool.XXXXXX.com;
                ## Your only path reference.
                root /wwwroot;
                ## This should be in your http block and if it is, it's not needed here.
                index index.php;

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

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

                location / {
                        # This is cool because no php is touched for static content.
                        # include the "?$args" part so non-default permalinks doesn't break when using query string
                        try_files $uri $uri/ /index.php?$args;
                }

                location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass XXXXweb-php: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 ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                        expires max;
                        log_not_found off;
                }
        }
}

我添加后

        server {
            listen 80;
            server_name jp-cool.XXXXXX.com;
            return 301 https://jp-cool.XXXXXX.com$request_uri;
        }

它适用于 http 不安全的连接。

然而,在我添加 cool.XXXXXX.comjp-cool.XXXXXX.com 重复部分后,只有其中一个可以工作。

我在设置 'different domain per language' 时在 WPML 面板上得到了 invalid

如果没有 docker,我可以在本地 nginx 中设置不同的域 /etc/nginx/site-available

但我无法使用 docker 化的 nginx 进行设置。

如果您没有通配符证书,您唯一的选择是为每个证书复制服务器块。方法如下:

        server {
                # this part changes per certificate
                ssl_certificate /etc/letsencrypt/live/cool.XXXXXX.com/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/cool.XXXXXX.com/privkey.pem; # managed by Certbot                
                server_name cool.XXXXXX.com;
                include common;
        }
        server {
                # this part changes per certificate
                ssl_certificate /etc/letsencrypt/live/jp-cool.XXXXXX.com/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/jp-cool.XXXXXX.com/privkey.pem;
                server_name jp-cool.XXXXXX.com;
                include common;
         }

为了遵循DRY原则,将服务器块的其余部分放入一个单独的文件中。我使用 'common' 作为该文件的名称,您需要将它放在 /etc/nginx/ 中,否则您必须在上面的块中更改路径。 /etc/nginx/common:

                listen [::]:443 ssl ipv6only=on; # managed by Certbot
                listen 443 ssl; # managed by Certbot
                root /wwwroot;
                ## This should be in your http block and if it is, it's not needed here.
                index index.php;

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

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

                location / {
                        # This is cool because no php is touched for static content.
                        # include the "?$args" part so non-default permalinks doesn't break when using query string
                        try_files $uri $uri/ /index.php?$args;
                }

                location ~ \.php$ {
                        try_files $uri =404;
                        fastcgi_split_path_info ^(.+\.php)(/.+)$;
                        fastcgi_pass XXXXweb-php: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 ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
                        expires max;
                        log_not_found off;
                }

您也可以使用一台服务器进行 HTTPS 重定向:

        server {
            listen 80;
            server_name cool.XXXXXX.com;
            server_name jp-cool.XXXXXX.com;
            return 301 https://$host$request_uri;
        }