简体   繁体   English

如何为在 docker 容器中运行的 wordpress 站点配置 Ngix?

[英]How to configure Ngix for wordpress site running ina docker container?

I am trying to run WordPress app using docker container on Nginx server.我正在尝试在 Nginx 服务器上使用 docker 容器运行 WordPress 应用程序。

I used the following docker-compose.yml file to run my WordPress locally我使用以下docker-compose.yml文件在本地运行我的 WordPress

version: "3"
services:

    wordpress_app:
        image: wordpress:latest
        restart: always
        container_name: wordpress_app
        environment:
            WORDPRESS_DB_HOST: mysql_server:3306
            WORDPRESS_DB_USER: db_username
            WORDPRESS_DB_PASSWORD: db_password
            WORDPRESS_DB_NAME: wordpress_app
        depends_on:
            - mysql_server
        volumes:
            - wordpress_app_public_html:/var/www/html
        ports:
            - "50001:80"
            - "50002:443"

    mysql_server:
        image: mysql:latest
        restart: always
        container_name: mysql_server
        environment:
            MYSQL_DATABASE: wordpress_app
            MYSQL_USER: db_username
            MYSQL_PASSWORD: db_password
            MYSQL_ROOT_PASSWORD: db_root_password
        volumes:
            - mysql_server_data:/var/lib/mysql

volumes:
    wordpress_app_public_html:
    mysql_server_data:

networks:
    default:
       external:
           name: wordpress-network

Now, I added a file called /etc/nginx/sites-available/usa.mydomain.com.conf with the following content现在,我添加了一个名为/etc/nginx/sites-available/usa.mydomain.com.conf的文件,其内容如下

server {
    listen        80;
    server_name   usa.mydomain.com;

    index index.php;

    access_log /var/log/nginx/usa.mydomain.com-access.log;
    error_log /var/log/nginx/usa.mydomain.com-error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass locahost:50001;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Then I added a virtual file of the same configuration into sites-enabled folder like this然后我将相同配置的虚拟文件添加到sites-enabled文件夹中,如下所示

cd /etc/nginx/sites-enabled
ln -s ../sites-available/usa.mydomain.com.conf .

But when I open usa.mydomain.com I get error但是当我打开 usa.mydomain.com 时出现错误

Problem Loading Page: The connection was reset加载页面问题:连接已重置

But when I change the config to the following the site works.但是当我将配置更改为以下内容时,站点就可以工作了。 But the html code resolve content to http://localhost:50001但是 html 代码将内容解析为http://localhost:50001

server {
    listen        80;
    server_name   usa.mydomain.com;

    location / {
        proxy_pass  http://localhost:50001;
    }

    index index.php;

    access_log /var/log/nginx/usa.mydomain.com-access.log;
    error_log /var/log/nginx/usa.mydomain.com-error.log;
}

So the page HTML source looks like this所以页面 HTML 源代码看起来像这样

<!DOCTYPE html>
<html lang="en-US" xml:lang="en-US">
<head>
    <meta name="viewport" content="width=device-width" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="robots" content="noindex,nofollow" />
    <title>WordPress &rsaquo; Installation</title>
    <link rel='stylesheet' id='dashicons-css'  href='http://localhost:50001/wp-includes/css/dashicons.min.css?ver=5.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='buttons-css'  href='http://localhost:50001/wp-includes/css/buttons.min.css?ver=5.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='forms-css'  href='http://localhost:50001/wp-admin/css/forms.min.css?ver=5.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='l10n-css'  href='http://localhost:50001/wp-admin/css/l10n.min.css?ver=5.5.1' type='text/css' media='all' />
    <link rel='stylesheet' id='install-css'  href='http://localhost:50001/wp-admin/css/install.min.css?ver=5.5.1' type='text/css' media='all' />

How can I correctly configure nginx site so that WordPress can work properly?如何正确配置 nginx 站点以便 WordPress 正常运行?

I finally figured out the issues.我终于弄清楚了问题所在。 Adding proxy_set_header HOST $host;添加proxy_set_header HOST $host; was the missing peace.是缺少的和平。 Below is my final/working config.以下是我的最终/工作配置。

server {
    listen 80;
    server_name usa.mydomain.com;

    root /data/wordpress_app/public_html;

    access_log off;

    error_log /var/log/nginx/wordpress_app-error.log;

    index index.html 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 ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
        log_not_found 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;
        proxy_pass http://localhost:8000;
        proxy_set_header HOST $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问在 docker 容器上运行的 wordpress? - How to access wordpress running on a docker container? 如何从同一台 wordpress 服务器(运行在同一台服务器上的 docker 容器)访问运行在 docker 容器中的数据库? - How to access the database running in a docker container from the same wordpress server(docker container running in same server)? 在 Docker 容器环境中设置 Wordpress 站点时出错 - Error setting Wordpress site in Docker container environment ngix + php fpm上的很多重定向链WordPress网站 - lots of redirect chains WordPress site on ngix + php fpm 在 Docker 容器中托管 WordPress 站点的视频 - Hosting video for a WordPress site in a Docker container 如何配置 WAMP 和 Wordpress 以进行定期站点维护 - How to configure WAMP and Wordpress for regular site maintenance 有没有办法向 docker 容器中的 wordpress 站点发出 postman 请求? - Is there a way to make a postman request to a wordpress site that is in docker container? Docker中的WordPress容器如何连接域名? - How to connect domain name with WordPress container in Docker? 运行 Wordpress Docker 容器时启用 EXIF 支持 - Enable EXIF support when running Wordpress Docker container 如何将 apache2 链接到 wordpress docker 容器? - How to link apache2 to a wordpress docker container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM