Rewrite requests to different enpoint with nginx - php

I have site on joomla and subfolder folder with standalone php files.
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
fastcgi_index index.php;
fastcgi_pass backend;
fastcgi_read_timeout 90;
}
Now I want to do something like this:
location ~ /forder/(.*)\.php {
try_files $uri /folder/app.php?controller=$1;
}
When I go to https://site/folder/script.php nginx send source code of that file.
Where I go wrong?

Solution for me is to put
if (!-e $request_filename){
rewrite /folder/(.*)\.php /folder/app.php?controller=$1 last;
}
in server section

Related

Nginx + Laravel - Moving blog from subdomain to /blog

My server application uses Laravel, and I have until recently had a wordpress blog hosted at blog.server.com
I wish to change this to server.com/blog
I have updated the main nginx config by adding the following:
location ^~ /blog {
root /home/ploi/blog.server.com/public;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php?$query_string;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_buffers 16 16k;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This kind of works. It loads the new /blog url and posts, but:
No static assets get loaded (js, png, css, etc)
server.com/blog/wp-admin/ loops infinitely
How can I solve this?
You could try this, worked for me:
location /blog {
alias /home/ploi/blog.server.com/public;
try_files $uri $uri/ #blogrewrite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
location #blogrewrite {
rewrite /blog/(.*)$ /blog/index.php?/$1 last;
}
I used a "named location" (#blogrewrite), but frankly I don't know why this was necessary, but it avoided the recursion.

Nginx with worpress as sub-directory returning 404 in posts

After adding various configurations into the Wordpress subdirectory, I can access without any issues the WP homepage, but I'm still not able to access the posts, returning 404.
www.test.com/blog - WP homepage, works perfectly fine
www.test.com/blog/test-post-for-blog - WP Post, 404
Here is the config:
server {
listen 80;
listen [::]:80;
server_name www.test.com;
location ^~ /media {
alias /var/local/test/static/media;
}
location ^~ /icons {
alias /var/local/test/static/icons;
}
location /blog {
alias /var/www/html;
index index.html index.htm index.php;
try_files $uri $uri/ /blog/index.php?$args /blog/index.php?q=$uri&$args;
# try_files $uri $uri/ /blog/index.php?q=$uri$args;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
And the log:
2022/02/09 14:47:25 [error] 32#32: *45 open() "/etc/nginx/html/index.php" failed (2: No such file or directory), client: 10.10.10.10, server: www.test.com, request: "GET /blog/test-post-for-blog/ HTTP/1.1", host: "www.test.com", referrer: "https://www.test.com/blog/"
10.10.10.10 - admin [09/Feb/2022:14:47:25 +0000] "GET /blog/test-post-for-blog/ HTTP/1.1" 404 187 "https://www.test.com/blog/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.87 Safari/537.36"
Checking the logs, it seems to look into the wrong place, which is /etc/nginx/html/index.php, but if I add the alias in the php location block, then the homepage will stop working as well, which gets me a bit confused.
Currently, I'm not quite sure if the problem is the alias in the blog block ( nginx recommends not using alias together with try_files, apparently due to a bug ), or anything else. If it is indeed the alias directive, I'll try to add root, together with some rewrite rules to avoid modifying the file structure.
It really took me more than it should to figure it out, and still can not actually understand what might be the issue here.
UPDATE 1 :
This is extremely odd. With the following blog config, separating the php location from the blog, the WP posts are accesible, but not the homepage and admin, returning 404:
location /blog {
alias /var/www/html;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
alias /var/www/html;
try_files $uri =404;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
UPDATE 2 :
Now this works ( the blog and the posts can be accesed ) , but the admin seems to be going into a redirect loop:
location /blog {
alias /var/www/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ \.php$ {
alias /var/www/html;
try_files $uri $uri/ /index.php?$args;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_index index.php;
fastcgi_pass blog-test:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
UPDATE 3 & FIX :
This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:
root /var/www/html;
index index.php;
location /blog {
rewrite ^/blog(.*)$ /$1 break;
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
rewrite ^/blog(.*)$ $1 break;
fastcgi_pass blog-test:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
Outside the /blog dir you might also have php files. There should be a separate block for the wordpress install that has the same settings, but then for the blog dir.
This could be the block for wordpress (tested in Docker)
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
So your complete nginx config would look like this:
server {
listen 80;
listen [::]:80;
server_name www.test.com;
root /var/www/html;
index index.php;
location ^~ /media {
alias /var/local/test/static/media;
}
location ^~ /icons {
alias /var/local/test/static/icons;
}
# This is for Wordpress
location /blog {
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
fastcgi_split_path_info ^/blog/(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
# This is for php files in the root. If there is no php to be parsed there you could leave these blocks out.
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass wp:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
UPDATE: I added a root and index property to the nginx server config.
UPDATE 3 & FIX :
This apparently works, but the config seems to be really unpleasant and unoptimised. If I try to nest the php location into the blog block, then the php files will download instead of rendering. If I try to use alias instead of root, some pages will not show resulting in 404. In any case, this seems to be functional:
root /var/www/html;
index index.php;
location /blog {
rewrite ^/blog(.*)$ /$1 break;
try_files $uri $uri/ /blog/index.php?$args;
}
location ~ \.php$ {
rewrite ^/blog(.*)$ $1 break;
fastcgi_pass blog-test:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_split_path_info ^(/blog)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
}

Nginx is downloading PHP files when trying to remove the extension

So, basically my current configuration is like below. Whenever I try and request the URL without the PHP extension it downloads it instead?
server {
listen [::]:80;
root myDirectory;
index index.php index.html;
server_name myDomain;
location / {
try_files $uri $uri/ $uri.php $uri.php$is_args$query_string =404;
}
location ~\.php$ {
include snippets/fastcgi-php.conf;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
location ~ /\ ht {
deny all;
}
}
I've already done what a lot of other answers in other questions have suggested like editing the php7.1-fpm php.ini file:
cgi.fix_pathinfo=0
Any ideas?
Your current use of try_files will not work. The file elements of the try_files statement are processed within the same location block, which is the wrong location for PHP files. See this document for more.
There are a number of solutions, but you could use a named location to perform an internal rewrite if the script file is found to exist.
For example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
if (-f $document_root$uri.php) { rewrite ^ $uri.php last; }
return 404;
}
location ~ \.php$ {
try_files $uri =404;
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
See this caution on the use of if.

Run HHVM and PHP simultaneously with Nginx

I've been running a magento site using HHVM and Nginx. So far I hadn't experienced any problems and all I got was a very welcomed noticeable performance boost.
However, I've just added a plugin which uses PHP functions which are unsupported by HHVM. The good news is that this plugin is only needed for one page. Bad news is I'm not configuring Nginx right to serve this page only with PHP.
The usual fallback trick used by some people using error directives does not work in this case because the page doesn't throw an error. It just doesn't work if HHVM is enabled.
Instead, I've tried to write many variations of location blocks for that particular page. None worked, and these are the two that I had thought would do the trick.
Is there a way to run PHP only for a specific page?
FAILED SOLUTION 1
location ~ /page/.+\.php${
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_pass unix:/var/run/php5-fpm.sock; ##Unix socket
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.(hh|php)$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $fastcgi_https;
include fastcgi_params;
}
FAILED SOLUTION 2 (with nested location)
location ~ /page/ {
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_pass unix:/var/run/php5-fpm.sock; ##Unix socket
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.(hh|php)$ {
if (!-e $request_filename) { rewrite / /index.php last; }
fastcgi_keep_conn on;
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $fastcgi_https;
include fastcgi_params;
}
Try using variable condition approach, it works for me, locations starting with /serve-with-php/ are being served with unix:/var/run/php5-fpm.sock, while all the others are being served with 127.0.0.1:9000
server {
root /home/vearutop/test-hhvm;
index index.php index.html index.htm;
error_log /var/log/nginx-error.log error;
charset utf-8;
server_name test-hhvm;
location / {
set $fcgi_worker 127.0.0.1:9000; # hhvm handle
try_files $uri $uri/ /index.php?$query_string;
}
location /serve-with-php/ {
set $fcgi_worker unix:/var/run/php5-fpm.sock; # php-fpm handle
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass $fcgi_worker;
fastcgi_index index.php;
include fastcgi_params;
}
}

Setting up Wordpress and Rails in Nginx

I have nginx set up for Rails application that has been working beautifully for me. Now I want to move my Wordpress blog from blog.website.com to website.com/blog, so web crawlers treat it as part of the site.
I created a symbolic link in my Rails app's public/ directory and added the following to my nginx configuration:
# Rails server
server {
root /project/path/current/public;
server_name project.com;
passenger_enabled on;
rails_env production;
client_max_body_size 20m;
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
location /blog {
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
This works great.
But what I'd like to do is skip the symbolic link part. I'd like nginx to route directly to the wordpress directory. I tried changing the /blog block to:
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
}
This works, as in I'm being correctly redirected to the wordpress folder but my server doesn't know what to do with php files and sends them to my browser as files to download.
I tried nesting the \.php$ location inside the /blog location but it results in 404 error.
What's wrong with the following piece and how to fix it?
location ^~ /blog {
root /home/ubuntu/apps/blog;
index index.html index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
if (!-e $request_filename) {
rewrite ^.+?(/blog/.*.php)$ $1 last;
rewrite ^ /blog/index.php last;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
I haven't fixed the snippet from the question but I managed to solve the problem with nginx configuration without using a symbolic link by using two server blocks.
server {
location / {
proxy_pass http://localhost:82/;
}
location /blog {
root /var/proj/blog/current;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
# Rails server
server {
listen 82;
root /var/proj/site/current/public;
server_name site.com;
passenger_enabled on;
rails_env staging;
client_max_body_size 20m;
# other configuration
}

Categories