首页 > 解决方案 > 如何配置使用 WPML 的“每种语言的不同域”的 dockerized wordpress nginx?

问题描述

现在这是一个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不安全的连接。

但是,在我添加 的jp-cool.XXXXXX.com重复部分后cool.XXXXXX.com,其中只有一个可以工作。

invalid在设置“每种语言的不同域”时,我进入了 WPML 面板。

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

但是我不能用 dockerized nginx 来设置它。

标签: wordpressdockernginxwpml

解决方案


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

        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;
        }

推荐阅读