首页 > 解决方案 > 使用 nginx 使用烧瓶应用程序启用 wordpress

问题描述

是否可以使用 nginx 在现有烧瓶应用程序的 /blog 上启用 wordpress?这是我一直在使用但没有得到任何结果的配置。我可以让烧瓶或 wordpress 通过 nginx 工作,但
1) 不能同时使用 2) 不是启用了 /blog 选项的 wordpress(wordpress 在 / 但不是 /blog 工作)

server {
    listen 80;
    server_name 0.0.0.0; 

#### if I enable the flask app, the blog doesn't work, so how can I keep this as well as add /blog ####
    # location / {
        #   include uwsgi_params;
        #   uwsgi_pass unix:/var/www/html/cr_webapp/my_app.sock;
    #}

#### if I change / to /blog, it starts looking in /usr/share/nginx/html location ####
    location = / {
        #root /var/www/html/blog;
        index index.php;
        try_files $uri $uri/ /blog/index.php?q=$uri?$args;
    }

    location = /favicon.ico {
        root /var/www/html/blog;
        log_not_found off;
        access_log off;
    }
    location = /robots.txt {
        root /var/www/html/blog;
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ \.php$ {
        root /var/www/html/blog;
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        root /var/www/html/blog;
        expires max;
        log_not_found off;
    }
}

我查看了一个链接,但该解决方案对我不起作用。另外,当我使用 /blog 时,它默认为 --prefix 位置,所以不知道如何更改它 -一个链接

我正在尝试做的事情可能吗?或者我一直无知。

标签: wordpressnginxflask

解决方案


要在前缀下运行 WordPress /blog,并假设它安装在同名目​​录中,则设置root为上述目录。

^~对属于 WordPress 的任何内容使用修饰符和嵌套位置块。有关详细信息,请参阅此文档

location ^~ /blog {
    root /var/www/html;
    index index.php;
    try_files $uri $uri/ /blog/index.php?q=$uri?$args;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
        expires max;
        log_not_found off;
    }
}

假设您现有的烧瓶配置有效,这些行应该没问题:

location / {
    include uwsgi_params;
    uwsgi_pass unix:/var/www/html/cr_webapp/my_app.sock;
}

以下几行与 WordPress 或烧瓶无关。如果文件存在,请设置一个root指向文件所在位置的值。

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

推荐阅读