2

I have some content in WordPress for pages with static content or Marketing landing pages. We decided to go on this way because of reasons...

So for doing that I have the configuration below in NGINX

server {
    server_name mydomain.com;

    location / {
        root /var/www/webapp;
        index index.html index.htm;

        try_files $uri /index.html;

        location ~* \.(eot|ttf|woff)$ {
            add_header Access-Control-Allow-Origin *;
        }

        access_log /var/log/nginx/webapp.access.log;
        error_log /var/log/nginx/webapp.error.log;

    }

    # Alias for discover when is Wordpress related content
    location /discover/ {

        index index.html index.htm index.php;
        root /var/www/wordpress;
        try_files $uri $uri/ /index.php$is_args$args;

        access_log /var/log/nginx/wordpress.access.log;
        error_log /var/log/nginx/wordpress.error.log;

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

    }

}

Sadly this is not working, it's always rendering or try to render the react app even when I go to mydomain.com/discover/ or /discover/something

I configured WordPress URL to mydomain.com/discover in the WP Configuration.

The error for this configuration is

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client:

If I move the PHP extension file configuration outside of /discover I remove the FastCGI Primary script unknown error but I still NGINX resolve everything to the react app.

Some I'm quite new with NGINX I've been using Apache my whole so might be a really noob error, but it's killing already.

I tried moving things around even as default but similar errors.

Any help or guide will help me.

  • BTW I have the WordPress project configured in the same server under other domain and works fine – Pablo Morales Aug 29 '18 at 5:51
  • Is WordPress installed under /var/www/wordpress/ or /var/www/wordpress/discover/? – Richard Smith Aug 29 '18 at 8:59
  • /var/www/wordpress – Pablo Morales Aug 29 '18 at 15:10
4

To access a path like /var/www/wordpress/ with a URI like /discover/, you will need to use alias rather than root. See this document for details.

For example:

location ^~ /discover {
    alias /var/www/wordpress;

    access_log /var/log/nginx/wordpress.access.log;
    error_log /var/log/nginx/wordpress.error.log;

    index index.php;
    if (!-e $request_filename) { rewrite ^ /discover/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }

        fastcgi_pass  unix:/var/run/php/php7.0-fpm.sock;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

The location value and the alias value should either both end with a / or neither end with a /.

Note that when using alias, $request_filename should be used to find the path to the filename.

There is a long standing bug related to using alias and try_files together, which is why these blocks use the if directive instead. Note this caution on the use of the if directive.

  • I made a copy and paste and work perfectly, thank you for the explanation and the solution. – Pablo Morales Aug 30 '18 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy