2

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 Answer 1

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;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried the above configurations. In this case only home page of the wordpress app works fine. Other pages of the wordpress app serve the index file of simple php app. For example BASE_URL/wordpress works fine. while, BASE_URL/wordpress/blog gives the same output as BASE_URL
Is WordPress configured to use the /wordpress prefix in SITEURL and HOME variables? I cannot see how /wordpress/blog could access /index.php
Yes, I configured WP_HOME and WP_SITEURL in configuration file. define('WP_HOME','localhost/wordpress'); define('WP_SITEURL','localhost/wordpress');
localhost is not correct - unless you only want to access the site locally. These variables need to specify BASE_URL or just /wordpress (to make it a relative URL)
Yes, I am installing the app in local machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.