【问题标题】:How to configure wordpress application with simple php application using Nginx?如何使用 Nginx 使用简单的 php 应用程序配置 wordpress 应用程序?
【发布时间】:2017-03-11 13:44:32
【问题描述】:

我想用一个简单的 php 应用程序配置一个 Wordpress 应用程序。 应用程序的目录结构如下:

根目录:/var/www/demoApp/

Wordpress 目录:/var/www/demoApp/wordpress/

在这里,我想使用路由 http://BASE_URL/wordpress 访问 wordpress 应用程序。但我无法配置 Web 服务器。 /var/www/demoApp/ 目录下的所有 php 页面都可以使用 url http://BASE_URL/ 正常工作。虽然没有正确加载 wordpress 文件。

这是我的 Nginx 配置块:

server 
{
    listen 80;
    root /var/www/demoApp;
    index index.php index.html index.htm;
    server_name localhost;

    error_page 500 404 /404.php;

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

    location /wordpress 
    {
        try_files $uri $uri/ /index.php?$query_string;
        rewrite ^(.*)$ /wordpress/index.php?$1 last;
        location ~ \.php 
        {
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            include /etc/nginx/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }

    location ~ \.php$ 
    {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

配置有什么问题?

【问题讨论】:

    标签: php wordpress nginx


    【解决方案1】:

    您对两个应用程序都使用了一个通用的root,因此嵌套的location ~ \.php 块是不必要的(并且在您的配置中永远不会被采用)。请参阅this document 了解更多信息。

    try_filesrewrite 有冲突,单个 try_files 语句就足够了。详情请见this document

    你应该试试这样的:

    server {
        listen 80;
        root /var/www/demoApp;
        index index.php index.html index.htm;
        server_name localhost;
    
        error_page 500 404 /404.php;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location /wordpress {
            try_files $uri $uri/ /wordpress/index.php;
        }
    
        location ~ \.php$ {
            try_files $uri =404;
    
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
    

    【讨论】:

    • 我试过上面的配置。在这种情况下,只有 wordpress 应用程序的主页可以正常工作。 wordpress 应用程序的其他页面提供简单 php 应用程序的索引文件。例如BASE_URL/wordpress 工作正常。而BASE_URL/wordpress/blog 提供与BASE_URL 相同的输出
    • WordPress 是否配置为使用SITEURL and HOME variables 中的/wordpress 前缀?我看不到/wordpress/blog 如何访问/index.php
    • 是的,我在配置文件中配置了 WP_HOME 和 WP_SITEURL。定义('WP_HOME','localhost/wordpress');定义('WP_SITEURL','localhost/wordpress');
    • localhost 不正确 - 除非您只想在本地访问该站点。这些变量需要指定 BASE_URL 或只是 /wordpress(使其成为相对 URL)
    • 是的,我正在本地机器上安装应用程序。
    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 2017-12-13
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多