Nitesh
Nitesh

Reputation: 1097

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

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?

Upvotes: 2

Views: 61

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

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

Upvotes: 1

Related Questions