wordpress - 404 page not found after updating permalinks w Nginx - wordpress

I changed my permalinks structure to use the postname
I restarted Nginx to clear the cache, but even if I can display my homepage, when I hit any oage link , I get a 404 page not found ...
Using Nginx , I don't have to update the .htaccess file , but should I update my site Nginx conf file ...
here is the location section used with .php
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; }
thanks for feedback

It's not a complete file section! server { } is missing.
Use this one but use this one between server { } and should work
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}

updated the /location to
location / {
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
working fine now ...

Related

allow localhost; deny all; for all despite "index.php" and "/"

I'm trying to restrict direct access to .php files on web server.
Used allow localhost;deny all;. However, this restricts access also to index.php.
How to overcome this issue? Is there something like IF conditions?
My config:
if ($request_uri ~* "^(/)index\.php$") {
return 301 $1;
}
location / {
try_files $uri $uri/ /index.php?$args;
rewrite ^/(\w+)$ /?system=$1 break;
rewrite ^/(\w+)/(\w+)(/.)*$ /?system=$1&id=$2 break;
rewrite ^/(.*)/$ /$1 permanent;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
}
What's commonly done is that you add a RewriteRule to an .htaccess file. This makes it possible to redirect all traffic to the index.php as you wish.
This solution even allows you to add the requested URL as an URL-parameter, so it is accessible within index.php as a $_GET variable.
Since .htaccess is only a solution for an Apache server it cannot be applied one-on-one here. This blog post on NGINX's website explains how it's done on NGINX: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
Instead of matching all PHP files, match just index.php and reject all others, like this:
location = /index.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
location ~ \.php$ {
return 301 $scheme://$http_host/index.php;
}
If you want to allow posts from the server to itself add the following for the URI
location = /post.php {
allow 127.0.0.1/24;
deny all;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
Edit: Alternative Configuration
server {
listen 80;
location = /index.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
location ~ \.php$ {
return 301 $scheme://$http_host/index.php;
}
}
server {
listen 127.0.0.1:81;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
}
}
Local requests would then need to be directed to port 81, ie:
curl http://localhost:81/myscript.php

Nginx rewrite downloading file

I am trying to rewrite a url with nginx but its downloading the php file instead of passing it
https://www.example.com/s.php?k=7eqx6si58Mn4fBf8n9oiF9lwIQ%3D%3D&b=5
Need to show as
https://www.example.com/s/7eqx6si58Mn4fBf8n9oiF9lwIQ%3D%3D&b=5
I tried the following but its not working
location ~ ^/s.php(.*)$ {
rewrite ^/s/$ /s.php?k= last;
}
I also have the following location block
location ~* \.php$ {
root /var/www/example.com/public_html/www;
try_files $uri =404;
fastcgi_pass unix:/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

How to use Nginx alias to load PHP from other directory?

I want to make a sub directory my on web server, system that serves a backend from another folder for people, but I am having some difficulty.
The server configuration should translate system as index.php of the /srv/www/xxx/backend/web, essentially system should alias to the index of another directory.
I have a configuration like:
location /system {
alias /srv/www/xxx/backend/web;
rewrite ^(.*) /index.php?r=$1;
return 200 $document_root$fastcgi_script_name;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location / {
rewrite /(.*) /index.php?r=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
I have tried numerous iterations (including using root), however even though I can get:
return 200 $document_root$fastcgi_script_name;
to give me:
/srv/www/xxx/backend/web/index.php
and I have vi'ed into this file to make sure it works when I take out the return wget gives me a 404. I am sure I am missing something really simple.
Can someone help me understand what is wrong?
As location php is nested the /index.php URI is not resolved here but in the last block of your configuration. Due to a long standing bug in nginx alias doesn't work with try_files so you need to use the root/rewrite couple instead. So you can fix this with :
location /system {
root /srv/www/xxx/backend/web;
rewrite ^/system/(.*)$ /$1 break;
try_files $uri /system/index.php?r=$uri;
location ~ \.php$ {
rewrite ^/system/(.*)$ /$1 break;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location / {
rewrite /(.*) /index.php?r=$1;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
To complete the accepted answer, I add some parts to get static files working:
location ~ ^/system(.*) {
root /srv/www/xxx/backend/web;
rewrite ^/system/(.*)$ /$1 break;
try_files $uri /system/index.php?r=$1&$args;
location ~ \.php$ {
rewrite ^/system/(.*)$ /$1 break;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ (.*\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar|mp4|ogg|woff|ttf))$ {
rewrite ^/system/(.*)$ /$1 break;
try_files $uri =404;
}
}
The last location solves the static files problem whereby files would not load from this place.

Nginx, PHP and rewrite

How do I make PHP-FPM rules play nicely with Nginx rewrite rules?
Current config file
server {
location / {
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi.conf;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?routestring=$1 break;
}
rewrite ^/(admincp/)$ /index.php?routestring=$1 break;
}
}
Change your location block to the following. Also try to avoid if statements, read about it (and more) here: http://wiki.nginx.org/Pitfalls
I've replaced the if (!-e ...) part with the #missing block in the config below.
server {
root /your/root/path
index index.php index.html index.htm;
server_name your.domain.com;
rewrite ^/(admincp/)$ /index.php?routestring=$1 break;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.php
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
# Move to the #missing part when the file doesn't exist
try_files $uri #missing;
# Fix for server variables that behave differently under nginx/$
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with ngingx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
# Pass to upstream PHP-FPM; This must match whater you name you$
#fastcgi_pass phpfpm;
fastcgi_pass 127.0.0.1:9000;
}
location #missing {
rewrite ^(.*)$ /index.php?routestring=$1 break;
}
}

nginx Wordpress URL rewrite

I've just installed nginx 1.0.8 and php-fpm and for the last 30 minutes I'm trying to rewrite the URL for Wordpress.
Here is what the Wordpress URL should look like:
http://localhost/website/blog/2011/10/sample-post/
I've looked at this tutorial: http://wiki.nginx.org/WordPress + many other on the web but every time I receive an 404 error (sometimes 403).
Here is what I did in my configuration file:
location /website/blog {
try_files $uri $uri/ /website/blog/index.php;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/website/blog)(/.*)$;
include fastcgi_params;
error_page 404 /404.html;
}
With this configuration I'm receiving "403 forbidden" status.
What am I missing?
Could be the permission of the root site folder
This is an example that i use to wordpress
server {
listen 80;
server_name www.mysite.com mysite.com;
root /srv/www/mysite.com/public_html;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
include /srv/www/mysite.com/public_html/*.conf;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Did you try restarting nginx after you have saved your config?
Also, check out my nginx/WordPress setup guide here:
http://themesforge.com/featured/high-performance-wordpress-part-3/

Resources