1

We have a wordpress install in a subfolder of the root domain which is in a different path, even when using root in the location, the wordpress install still loads the root of the domains site.

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

server {
  listen              443 ssl;
  server_name         domain.com;

  ssl                 on;
  #ssl_certificate     /var/www/certs/star.domain.com.cert;
  #ssl_certificate_key /var/www/certs/star.domain.com.key;
  ssl_certificate      /var/www/certs/cert_chain.crt;
  ssl_certificate_key  /var/www/certs/__domain_com.key;
  ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers         ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;

  return              301 https://www.$server_name$request_uri;
}

server {
  ## Default Server Settings
  include             /etc/nginx/basic.conf;

  listen              443 ssl;
  server_name         www.domain.com;

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

  root                /var/www/domain/current;
  index               index.php index.html;

  ssl                 on;
  #ssl_certificate     /var/www/certs/star.domain.com.cert;
  #ssl_certificate_key /var/www/certs/star.domain.com.key;
  ssl_certificate      /var/www/certs/cert_chain.crt;
  ssl_certificate_key  /var/www/certs/__domain_com.key;
  ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers         ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;

  error_page          404 /index.php;

  location / {
    try_files $uri $uri/ $uri.php?$query_string;
  }

location /blog {
    root /var/www/;
    try_files $uri $uri/ /blog/index.php?q=$request_uri;
}

location ~ /blog/.+\.php$ {
  try_files $uri $uri/ /blog/index.php?q=$request_uri =404;
  include       /etc/nginx/fastcgi_params.conf;
  fastcgi_pass  127.0.0.1:9000;
  fastcgi_index index.php;
}

}

This should work according to https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block

Here is the curl request to the root of the domain:

[root@callloop-web-1 www]# curl -IL callloop.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:31:27 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://callloop.com/

HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:31:27 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://www.callloop.com/

HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:31:28 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=3obiecqr3rm4i1q1bas8rcripdfuv8ru; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

And the access logs:

104.236.27.189 - - [21/Apr/2017:19:31:28 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.29.0"

and now the subfolder:

[root@callloop-web-1 www]# curl -IL callloop.com/blog
HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:33:27 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://callloop.com/blog

HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:33:27 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://www.callloop.com/blog

HTTP/1.1 301 Moved Permanently
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:33:27 GMT
Content-Type: text/html
Content-Length: 184
Location: https://www.callloop.com/blog/
Connection: keep-alive

HTTP/1.1 404 Not Found
Server: nginx/1.6.3
Date: Fri, 21 Apr 2017 19:33:27 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=1lo0hrgkcrsrtstlldnr8gr3o2vu20if; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache

access logs

104.236.27.189 - - [21/Apr/2017:19:33:27 +0000] "HEAD /blog HTTP/1.1" 301 0 "-" "curl/7.29.0"
104.236.27.189 - - [21/Apr/2017:19:33:27 +0000] "HEAD /blog/ HTTP/1.1" 404 0 "-" "curl/7.29.0"

So you can see a 404, makes sense but I have no idea WHY

5
  • First glance it looks ok. Can you please curl first the home page on the root folder, then the blog home page. With each curl please include the matching access log and any matching error log. Please make it easy for people to read, so edit your question and show the curl command line with corresponding logs, don't just dump a bunch of logs for us to figure out. Use example.com if your blog is secret or internal, but post the URL if you want more help.
    – Tim
    Apr 21, 2017 at 19:18
  • @Tim I've added the request. Nothing in the error logs. No idea why I'd be getting a 404 so its obviously looking in the wrong location, just seems like root /var/www/; is being ignored. The full path is /var/www/blog
    – JCats
    Apr 21, 2017 at 19:36
  • You are missing a root /var/www/; statement in the location ~ /blog/.+\.php$ block. Apr 21, 2017 at 20:26
  • @RichardSmith can you explain why that's needed, given there's a root in the server block? I run a website with the blog in a subdirectory and that only has a single root for the server. The key to my blog was the line "try_files $uri $uri/ /blog/index.php?$args;"
    – Tim
    Apr 21, 2017 at 20:28
  • @Tim he is using two roots, the last two location blocks refer to a different root. I have recommended using nested location blocks. Apr 21, 2017 at 20:35

2 Answers 2

6

You are missing a root statement in your location ~ /blog/.+\.php$ block. But you may find it easier to read if you nest the location blocks:

location ^~ /blog {
    root /var/www/;
    try_files $uri $uri/ /blog/index.php?q=$request_uri;

    location ~ \.php$ {
        try_files $uri =404;
        include       /etc/nginx/fastcgi_params.conf;
        fastcgi_pass  127.0.0.1:9000;
    }
}
1
  • Such a lifesaver! Thank you bro, I was struggling with it for 2 days Sep 28, 2021 at 7:27
0
server {
        listen       80;
        server_name  www.example.com example.com;
    root        /var/www/deploy/com-ang/;

    client_max_body_size 50M;
    large_client_header_buffers 4 16k;


        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                proxy_pass http://localhost:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
                #try_files $uri $uri/ =404;
    }

        location ^~ /blog {
                root /var/www/blog/;
                #alias /var/www/blog;
                #index index.php index.html index.htm;
                #try_files $uri $uri/ /blog/index.php?q=$request_uri;
        try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;

            #location ^~ /blog/.+\.php$ {
            location ~ \.php$ {
                    #try_files $uri $uri/ /blog/index.php?q=$request_uri =404;
                    try_files $uri =404;
                    include       /etc/nginx/fastcgi_params;
                    fastcgi_pass  127.0.0.1:9000;
                    #fastcgi_pass  unix:/var/run/php-fpm/php-fpm-7.0.sock;
                    #fastcgi_index index.php;
            }
    }

    # redirect server error pages to the static page /40x.html
        #
#        error_page 404 /404.html;
#            location = /40x.html {
#        }

        # redirect server error pages to the static page /50x.html
        #
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
}
2
  • Welcome to Server Fault. A little explanation what you changed in the configuration and why would really improve this answer. Jul 30, 2019 at 14:01
  • The configuration is correct but it doesn't worked for me. I have changed the #try files under the blog section, given a sub-directory and it worked. Thank you very much
    – SibaP
    Aug 16, 2019 at 7:45

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.