nginx rewrite rules subfolder to file - nginx

I have dynamic files on the server:
allen.php
bob.php
candy321.php
david.php
and I would like to add rewrite rules in Nginx, expected the URLs are:
http://example.com/allen
https://example.com/bob/?param=value
http://example.com/candy321/
https://example.com/david.php?param=value
I found a similar one, but I have no idea how to modify it to fit my case:
Nginx Rewrite Location to subfolders

You can try a modified version of this answer:
root /path/to/your/webroot;
index index.php index.html;
location / {
try_files $uri $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)/$ $1.php last;
rewrite ^ $uri.php last;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass ... # path to your PHP-FPM socket file or listening IP address/port
}

Related

Remove file extension and keep parameters on nginx

I'm trying to make url on my server seo friendly.
Original: /face.php?name=hello-stackoverflow123
Desired: /face/hello-stackoverflow123
I made those little configs:
location / {
try_files $uri $uri.html $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Problem 1: I can't find a config to keep the parameters it gives me always 404 (so I get 404 if I try face/hello-stackoverflow123
Problem 2: If I have a file faces.php it will open if I try/faces, but if I open faces.php it won't redirect to /faces

Nginx install wordpress under some URI prefix on existing website

We have a website build with react by a partener. And we want to add a blog on the same domain, in a subdirectory:
example.org => /var/www/example.org/app
example.org/blog => /var/www/example.org/blog
I tried many solutions, but I always got a 404 on example.org/blog
server {
server_name example.org;
root /var/www/project/app/functions/build;
access_log /var/log/nginx/example.org_access.log;
error_log /var/log/nginx/example.org_error.log;
index index.html index.htm;
location ^~ /blog/ {
access_log /var/log/nginx/blog-example.org_access.log;
error_log /var/log/nginx/blog-example.orgm_error.log;
alias /var/www/example.org/blog;
index /index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /index.php?$args;
}
location / {
try_files $uri #prerender;
}
location #prerender {
// .. config for react stuff
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
I tried to create a subdomain with a separate nginx site conf to test if everything was OK, and it was. So it's my nginx site config above which is not good.
Do you have any idea?
Thanks you!
Solution
Thanks to Ivan Shatsky, I was able to correct my config to make everything works.
My main issue why I always had a 404 was because of my index. I had an extra slash: index /index.php => index index.php
location /blog/ {
access_log /var/log/nginx/blog-example.org_access.log;
error_log /var/log/nginx/blog-example.org_error.log;
root /var/www/example.org;
index index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
Not sure if this will be an answer, but the errors I already see are:
If your react app doesn't make use of PHP (and I suppose it doesn't) why are you use location ^~ /blog/ { ... } having a PHP handler below this block? This way no request for /blog/any/path/file.php ever reach that PHP handler. Use simple location /blog/ { ... } prefix location or move the PHP handler inside the location ^~ /blog/ { ... } making it nested location (preferred).
If your directory where the WP is located is /var/www/example.org/blog and your URI pefix is /blog/ you'd better use root /var/www/example.org; instead of alias /var/www/example.org/blog;. It those string doesn't match, add the trailing slash at the end of the alias directive argument: alias /var/www/example.org/blog/;
You are using try_files directive incorectly, the last argument supposed to be an URI, so to redirect all the requests to WP index file you should use try_files $uri $uri/ /blog/index.php?$args;
Your PHP handler uses global /var/www/project/app/functions/build root instead of WordPress one (if you'd make the PHP handler nested location, this one would gone automatically).
Not sure that's all, but lets start from fixing these errors.
Update
The final working configuration was added by OP as the original question update.

Nginx rewrite in subfolder (404)

I has a site host on a NGINX server which used to work fine to remove index.php in nginx site config using try_files.
But now I am going to add a blog on it, where the URL will be www.foo.com/blog, I can access the blog and use index.php?p=.
But, once I use pretty permalink with Nginx Helper, www.foo.com/blog/2013/07/bar, I get 404.
server {
# don't forget to tell on which port this server listens
listen 80;
# listen on the www host
server_name foo.com;
# and redirect to the non-www host (declared below)
return 301 $scheme://www.ultra-case.com$request_uri;
}
server {
# listen 80 default_server deferred; # for Linux
# listen 80 default_server accept_filter=httpready; # for FreeBSD
listen 80;
# The host name to respond to
server_name www.foo.com;
# Path for static files
root /web/foo.com
#index file
index index.php;
#Specify a charset
charset utf-8;
# Custom 404 page
error_page 404 /404.html;
# Uri Rewrite
location /blog {
index index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
location / {
autoindex on;
# This is cool because no php is touched for static content.
# include tihe "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# Include the component config parts for h5bp
include conf/h5bp.conf;
}
The accepted answer routes everything through index.php.
This will break certain script includes, the wp-admin script being one of them.
You can use:
location /blog/ {
index index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
Um... Thank you for all comments and answer. But finally I use this method to get it works
location /blog {
index index.php;
rewrite ^/blog/(.*)+$ /blog/index.php?$1; # it finally works
# return 200 $request_uri; # it is for inspect what $request_uri is
# try_files $uri $uri/ /blog/index.php$request_uri$is_args$args; # it gets 500 server error
}
Please point out if current setting has any problems. thank you!
I would suggest the following, to catch any permalinks under subfolder /blog
location /blog {
index index.php;
try_files $uri $uri/ /blog/index.php?$args;
}
Try this, I changed my answer to try to imitate the same behaviour you are using in your rewrite.
location ~ /blog(.*) {
index index.php;
try_files $uri /blog/index.php?$1&$args;
}
Try this
location /api {
# example: http://demo.com/api/channels/dmzb
root /data/webserver/demo.com/api/web;
rewrite ^/api/(.*) /$1 break;
try_files $uri $uri/ /api/index.php?$args;
location ~ ^/api/index\.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
# fix request_uri
set $changed_request_uri $request_uri;
if ($changed_request_uri ~ ^/api(.*)) {
set $changed_request_uri $1;
}
fastcgi_param REQUEST_URI $changed_request_uri;
# fix script_filename
fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*);
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
Think for php, rewrite is no needed with something like this:
location /app/ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/your/app/index.php;
fastcgi_pass php;
}
With following fastcgi pass
upstream php {
server unix:/var/run/php5-fpm.sock;
}
A universal solution for pretty URLs in root and one subfolder level:
set $virtualdir "";
set $realdir "";
if ($request_uri ~ ^/([^/]*)/.*$ ) {
set $virtualdir /$1;
}
if (-d "$document_root$virtualdir") {
set $realdir "${virtualdir}";
}
location / {
try_files $uri $uri/ $realdir/index.php?$args;
}
I found that with permalink enabled, I needed a combination of both sets of answers given here, otherwise
With only the rewrite, none of the static files got served
With only the try files, the permalinks did not work
This is working on my set up
location /blog/ {
rewrite ^/blog/(blog/(tag|category|20??)/.*)+$ /blog/index.php?$1;
try_files $uri $uri/ /blog/index.php?$args =404;
}
ip url: 123.123.123/xxxxxxxxxx/
location /xxxxxxxxxx/ {
try_files $uri $uri/ /xxxxxxxxxx/index.php?$query_string;
}
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite ^(/xxxxxxxxxx/.*)+(/wp-.*) /xxxxxxxxxx/$2 last;
rewrite ^(/xxxxxxxxxx/.*)+.*(/wp-admin/.*\.php)$ /xxxxxxxxxx/$2 last;
rewrite ^(/xxxxxxxxxx/.*)+(/.*\.php)$ /xxxxxxxxxx/$2 last;
}

URL Parameters and Rewrite

I am somewhat new to nginx and am having a hard time with the rewrites. I am trying to get:
/c/545_453453_4534
to access c.php passing 545_453453_4534 in as params
Here is my current conf:
location / {
try_files $uri $uri/ #extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:8000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Thanks!
In your NGINX server block you need to add:
rewrite ^/c/([^/]*)$ /c.php?param=$1 last;
What I tend to do is generate my re-write Apache style then convert it to NGINX format using:
http://www.anilcetin.com

Can't serve "hidden" wordpress install

I'm switching a Wordpress site from Apache to Nginx.
We're trying to obfuscate the fact that it's a Wordpress site (I don't know why, but those are the requirements).
There are a few calls to static assets within the wordpress directory, so it's not perfect.
For the most part, it's working (www.example.com and other pages within show up properly), but I can't get the Admin page to work.
When you go to example.com/wp-admin, the server should load /var/example.com/docroot/wordpress/wp-admin/index.php, but I keep getting a 404.
I'm not sure where I've gone wrong. This is my server conf:
server {
server_name *.example.com;
root /var/example.com/docroot;
access_log logs/access.log;
error_log logs/error.log debug ;
gzip off;
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Rewrite wp-content to pretend we don't use Wordpress
location ~ ^/content/(.*)$ {
alias /var/example.com/docroot/wordpress/wp-content/$1;
}
location ~ ^/wp-content/(.*)$ {
alias /var/example.com/docroot/wordpress/wp-content/$1;
}
location ~ ^/includes/(.*)$ {
alias /var/example.com/docroot/wordpress/wp-includes/$1;
}
location ~ ^/wp-includes/(.*)$ {
alias /var/example.com/docroot/wordpress/wp-includes/$1;
}
location /wp-admin {
alias /var/example.com/docroot/wordpress/wp-admin;
}
# Basic php configuration
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# static assets and blocking specific access
include sites-enabled/common.conf;
}
The two things I did to get this to work:
Proxy_pass instead of alias
location /wp-admin {
proxy_pass $scheme://www.dev4.railrode.net/wordpress/wp-admin/;
}
And for the try_files directives, added a wordpress$uri option:
try_files $uri /wordpress$uri =404

Resources