3

I've been running a site on puma / rails setup under nginx. Now we have to setup wordpress under path /blog, i.e.

www.mysite.com is the rails app, and www.mysite.com/blog is the wordpress blog.

the location setup done for Wordpress blog is as follows

upstream app {
    # Path to Puma SOCK file, as defined previously
    server unix:///var/run/website/website.sock fail_timeout=0;
}

server {
    listen 80;
    server_name www.bhokaal.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl http2;
    #server_name www.bhokaal.com;

    access_log /home/bhokaal/bhokaal.com/logs/access.log;
    error_log /home/bhokaal/bhokaal.com/logs/error.log;

    ssl_certificate /etc/ssl/www.bhokaal.com_cert_chain.crt;
    ssl_certificate_key /etc/ssl/www.bhokaal.com.key;

    root /home/bhokaal/bhokaal-website/public/;

    try_files $uri/index.html $uri @app;

    location @app {
        proxy_pass http://app;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

    location ^~ /assets/ {
        gzip_static on;
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
        add_header Cache-Control public;
    }

    location /blog {
       root /home/bhokaal/bhokaal-blog/;
       try_files $uri $uri/ /index.php?$args;
    }

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

    error_page 500 502 503 504 /500.html;
    client_max_body_size 500m;
    keepalive_timeout 15;
}

my assumption is that /blog get's processed by PHP while rest goes to upstream puma server.

We get a rather cryptic error when we access www.bhokaal.com/blog

2017/12/08 12:10:02 [error] 19193#19193: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 49.207.56.120, server: , request: "GET /blog/ HTTP/2.0", upstream: "fastcgi://unix:/run/php/php7.1-fpm.sock:", host: "www.bhokaal.com"

Nginx, PHP-FPM and Puma are all running under the same user, so I don't think it is user permissions problem as pointed out by most of blogs I referred on this problem.

2
  • 1
    Is WordPress installed into a directory called blog? i.e. /home/bhokaal/bhokaal-blog/blog/? Dec 8, 2017 at 10:21
  • wordpress is installed in /home/bhokaal/bhokaal-blog/ Dec 8, 2017 at 12:40

2 Answers 2

2

You need to use alias to map URIs which begin with /blog to a root that ends with something else. See this document for more.

The cleanest solution is to use a nested location block. The ^~ modifier on the outer block avoids any side-effects. See this document for more.

location ^~ /blog {
    alias /home/bhokaal/bhokaal-blog;
    index index.php;

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

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass unix:/run/php/php7.1-fpm.sock;

        include /etc/nginx/fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
    }
}

Avoid try_files with alias due to this issue, and note this caution on the use of if.

8
  • what does this line mean if (!-f $request_filename) { return 404; } and why do we need it? same for if (!-e $request_filename) { rewrite ^ /blog/index.php last; } Dec 8, 2017 at 18:00
  • it's become a bit more confusing now that after applying this config I get a 403 on /blog Dec 8, 2017 at 18:07
  • I missed out the index index.php; statement. See updated answer. Dec 8, 2017 at 18:10
  • The if (!-f $request_filename) { return 404; } statement is in response to this issue, but using if instead of try_files. Dec 8, 2017 at 18:12
  • okay so we're saying "if the request is not a directory try rewriting to blog/index.php as last option?" and if the file name does not exist return a 404? Dec 8, 2017 at 18:17
0

this is what worked for me,

installing wordpress under /home/bhokaal/bhokaal-website/public/blog i.e. the rails public folder

adding the location and PHP blocks as usual

location  /blog {
    index   index.html index.htm index.php;
    try_files $uri $uri/ /blog/index.php?$args;
}

location ~ \.php$ {
    try_files $uri =404;

    fastcgi_pass    unix:/run/php/php7.1-fpm.sock;
    fastcgi_index   index.php;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .