Unable to get location blocks in nginx

Having difficulty trying to determine location blocks in nginx configuration. This is what I have:

server { listen 80; server_name _; access_log /var/log/nginx/example.com.access_log; error_log /var/log/nginx/example.com.error_log warn; root /var/www/root; index index.php index.htm index.html; fastcgi_index index.php; location /wp/ { root /var/www/wordpress; index index.php index.htm index.html; fastcgi_index index.php; } location ~* \.php$ { try_files $uri =404; keepalive_timeout 0; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } } 

Browsing in / works as expected and shows the website in / var / www / root, but if the locations work, since I think they should browse / wp, I have to go on to install wordpress in / var / www / wordpress. All I get is:

404 Not Found

Nginx / 0.7.67

If I move the directory / var / www / wordpress to / var / www / root / wordpress and browse / wordpress, all this is perfect.

What am I doing wrong with the location block?

I have never configured nginx before, and in any case, I'm a little full webbb.

I want to have more location blocks for other applications. This is really just a basic example to post here.

Updated nginx to version in Debian Squeeze backports. No improvement:

404 Not Found

Nginx / 1.1.19

+6
source share
1 answer

The reason it doesn't work is because ...

At the server level, you have "root / var / www / root". Thus, each location block will use it, unless it is overridden. This is a good practice.

Then you redefined it in the location block β€œwp” to β€œ/ var / www / wordpress”. However, the php location block still uses the default value.

Now, when you place a request on "/wp/folder_a/file_a.php", which is physically located in "/var/www/wordpress/folder_a/file_a.php", the request falls into the php location block and the root folder that is active for of this block, it searches for the file in "/var/www/root/folder_a/file_a.php". As a result, you get "404 not found."

You can change the server level root directive to "/ var / www / wordpress" and remove the override at the wp location. This will solve this problem, but php scripts under "/ var / www / root" will no longer work. Not sure what you have.

If you need to run php under "/ var / www / root" and "/ var / www / wordpress", you need to do this:

 server { ... root /var/www/root; index index.php index.htm index.html; # Keep fastcgi directives together under location # so removed fastcgi_index # Put keepalive_timeout under 'http' section if possible location /wp/ { root /var/www/wordpress; # One appearance of 'index' under server block is sufficient location ~* \.php$ { try_files $uri =404; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } } location ~* \.php$ { try_files $uri =404; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } } 

That is, in the wp location block, the php location duplication block is located. It inherits the root directive for wp.

To help save succes and facilitate editing, etc., you can put the fastcgi directives in a separate file and include it as needed.

So in /path/fastcgi.params you have:

  fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; 

Now your conf could be:

 server { ... root /var/www/root; ... location /wp/ { root /var/www/wordpress; location ~* \.php$ { try_files $uri =404; include /path/fastcgi.params; } } location ~* \.php$ { try_files $uri =404; include /path/fastcgi.params; } } 

Thus, if you need to edit any fastcgi parameter, you will simply edit it in one place.

PS. Updating nginx will not solve the problem, since it is not a problem with the version, but the update anyway!

+7
source

Source: https://habr.com/ru/post/914602/


All Articles