0

The environment is as follows:

I have https://website.com and a blog at https://website.com/blog

The root path points to a Passenger-hosted Rails app, and the blog subdirectory points to a WordPress app via php-fpm

Everything works fine with my Nginx config, but when I try to change the permalink structure to anything other than "Plain", I get a 404 page from the Rails app as if the location blocks aren't utilized. I tried looking at the error log in debug mode, and I do see it attempting to try_files, but ultimately it fails with the Rails 404 page.

It may be worth noting that the entire site is behind Cloudflare. Not sure if it could be something with that, though I kind of doubt it.

Here is the almost-working Nginx config I'm using:

server {
    listen 80 default_server;
    server_name IP_ADDRESS;

    passenger_enabled on;
    passenger_app_env production;
    passenger_ruby /home/ubuntu/.rbenv/shims/ruby;

    root /web/rails/public;

    client_max_body_size 20M;

    location ^~ /blog {
                passenger_enabled off;

                alias /web/blog;
                index index.php index.htm index.html;

                # Tried the commented line below, but then nothing works.
                # try_files $uri $uri/ /blog/index.php?$args;
                # The line below works, but peramlinks don't.
                try_files $uri $uri/ /blog/index.php?q=$uri&$args;

                location ~ \.php$ {
                        include fastcgi_params;
                        fastcgi_pass unix:/run/php/php7.3-fpm.sock;

                        # Tried the commented line below, but then nothing works
                        # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        # The line below works, but peramlinks don't.
                        fastcgi_param SCRIPT_FILENAME $request_filename;
                }
        }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ethan B.
  • 1
  • 1

1 Answers1

0

I wanted to comment in short but I don't have enough reputation for that.

  • I used the following block and worked for me. I added an add_header directive just to debug that if my request is reaching the correct block.
location ^~ /blog {
    try_files $uri $uri/ /index.php?$args;
    add_header reached blog;
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass php;
        }
}
  • If your server is behind CloudFlare, you can try with /etc/hosts entry on your local machine if you're using Ubuntu/Mac. Which will stop the DNS lookup and site will directly be accessed from the IP address.

  • Check if any redirects are happening due to any other Nginx configuration.

  • Also, you have mentioned in the question that site is https:// while your server block has only listen 80 meaning non HTTPS.

  • Check for the response headers with

    curl -XGET -IL site-name.tld
    

    which may help you more debugging the situation.

  • Difference between alias and root directives https://stackoverflow.com/a/10647080/12257950