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;
}
Related
I am having an issue running /folder from a different path than the main website.
my nginx.conf for that section look like this:
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location ~ /folder {
alias /srv/http/folder;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}
}
location #folder {
rewrite /folder/(.*)$ /folder/index.php?/$1 last;
}
In the error.log I can see the following:
2020/06/03 09:05:26 [error] 25966#25966: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.21.2.46, server: example.com, request: "GET /folder/xxx_v6.15.11/Resources/images/redcaplogo.gif HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "example.com"
Any suggestion how to fix this?
Regex locations are matched from first to last, and the first found match processed by nginx. When you get a request for /folder/script.php, it is processed by the first location block. Try to swap this two locations. Additionaly, why are you do not include fastcgi_params in your second location block?
Update
I did some debugging, lets look at you code (assuming location block already swapped):
location ~ /folder {
alias /srv/http/folder;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location #folder {
rewrite /folder/(.*)$ /folder/index.php?/$1 last;
}
If we get a request /folder/some/path (and there is no some/path file or directory inside the folder), inside the nested location internal nginx variables would have following values:
$request_filename: empty string (there is no real file or folder /some/path inside th folder directory);
$uri: /folder/check.php;
$document_root: /srv/http/folder.
If we get a request /folder/some/path/script.php (and there is a real PHP script with this name), inside the nested location internal nginx variables would have following values:
$request_filename: /srv/http/folder/some/path/script.php;
$uri: /folder/some/path/check.php;
$document_root: /srv/http/folder.
Additionally, when you get a request for a static resource from your folder, for example /folder/some/path/static.css, the try_files $uri ... directive will search folder/some/path/static.css file in a /srv/http/folder directory, which leads to check the existence of /srv/http/folder/folder/some/path/static.css file.
One of possible solutions to get a file subpath inside the folder directory:
location ~ ^/folder(?<subpath>.*) {
alias /srv/http/folder;
try_files $subpath $subpath/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$subpath;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
include fastcgi.conf;
}
location #folder {
rewrite ^/folder/(.*)$ /folder/index.php?/$1 last;
}
This could be simplified if your real folder directory name is the same as your /folder URI prefix:
location ~ ^/folder {
root /srv/http;
try_files $uri $uri/ #folder;
location ~ \.php$ {
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
...
As nginx documentation states:
When location matches the last part of the directive’s value:
location /images/ {
alias /data/w3/images/;
}
it is better to use the root directive instead:
location /images/ {
root /data/w3;
}
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 ...
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.
My folder structure is as follows:
/www
/api.domain.tld
/app.domain.tld
The API contains the system it self and APP implements the API via HTTP.
I want to create an Nginx server for app.domain.tld that also contains an "virtual directory" for API.
You can contact the API likes this: http://api.domain.tld/method/api.json
But it would be great if the API can be contacted like this also: http://app.domain.tld/api/method/api.json without copying something into APP, but keep those two "systems" separated.
What I have for now:
server {
listen 80;
root /var/www/app.domain.tld;
index index.php index.html;
server_name app.domain.tld;
location ^~ /api {
alias /var/www/api.domain.tld;
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;
}
rewrite ^/api/([a-z]+)/api.json$ /api.php?method=$1 last;
}
location....
location....
location....
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;
}
}
Unfortunately this does now works as expected.
The rewrite does not work at all. I can get api.domain.tld/index.php but when it needs to use the rewrite, it will not work.
I have tried several things. Either I get 404 or 403 with this error:
directory index of [path] is forbidden
Can someone come up with a better solution that actually works?
Regards
You should change SCRIPT_FILENAME path:
server {
listen 80;
root /var/www/app.domain.tld;
index index.php index.html;
location ~ ^/api/(.+\.php)$ {
alias /www/api.domain.tld/public/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
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;
}
}