How to configure wordpress application with simple php application using Nginx?

五迷三道 提交于 2021-02-10 07:39:09

问题


I want to configure a Wordpress application with a simple php application. The directory structure of the application is as follow :

Root directory : /var/www/demoApp/

Wordpress directory : /var/www/demoApp/wordpress/

Here i want to access the wordpress application using route http://BASE_URL/wordpress. But i am not able to configure the web server. All the php pages under /var/www/demoApp/ directory are working fine using url http://BASE_URL/. While wordpress files are not being loaded correctly.

Here is my Nginx configuration block :

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

What could be wrong with the configuration?


回答1:


You are using a common root for both applications, therefore the nested location ~ \.php block is unnecessary (and in your configuration is never taken). See this document for more.

The try_files and rewrite are conflicting, and a single try_files statement is adequate. See this document for details.

You should try something like this:

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


来源:https://stackoverflow.com/questions/42735932/how-to-configure-wordpress-application-with-simple-php-application-using-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!