In Apache, I can access php scripts through an uri like /index.php/dashboard,
how can I set nginx to act the same?
Also I can access /index with Apache and it automatically maps to /index.php.
Is this also possible in nginx?
I think something like this is the solution:
map $uri $myvalue {
/index.php/(.*) /index.php?$;
}
Or is there a solution w/o rewrite?
My actual config is this:
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
I don't know if you pass the URI as a query string or what, but if that's the case here's what you could try
location ~ /index.php(.*) {
try_files /index.php?$1 =404;
}
Related
I'm pretty new to nginx.
I have two types of code one is simple php running through, index2.php and I have one directory named wordpress inside it has a whole wordpress website.
What I'm trying to achieve is to get them both running on the main domain without slashes with subdirectory names.
location ~ (/lottery-results|patternTwo) {
try_files $uri /index2.php$is_args$args;
}
location / {
try_files $uri /wordpress/index.php?$args;
}
This is the config I am currently using it works fine for my purpose.
The first directive loads some urls through simple php in index2.php.
The second directive loads wordpress, however when I open this url:
http://domain.test/
It send me to:
http://domain.test/wordpress/wp-admin/setup-config.php
My problem is that /wordpress part I want the url to be:
http://domain.test/wp-admin/setup-config.php
instead.
I tried to use alias and root but it didn't change anything, (honestly I don't get what alias and root even does!)
edit : PHP-FPM handler:
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /Applications/MAMP/conf/nginx/fastcgi_params;
}
Lets try this:
# This block should be OUTSIDE the server block!
map $request_uri $new_uri {
~^/wordpress(/.*) $1;
default $request_uri;
}
server {
...
location ~ (/lottery-results|patternTwo) {
try_files $uri /index2.php$is_args$args;
}
location / {
try_files $uri /wordpress$uri /wordpress/index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
# Here the order of directives DOES matter
# This should be the first:
include /Applications/MAMP/conf/nginx/fastcgi_params;
# This should be the second:
fastcgi_param REQUEST_URI $new_uri;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/Applications/MAMP/Library/logs/fastcgi/nginxFastCGI_phpMAMP_PhpLocalhost_MAMP.sock;
}
}
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.
I've got a website using nginx that's got a load of redirects to retain the juice and UI that old backlinks give me but they're not all working as I need them to. I suspect it could be an ordering issue but I'm not sure so I'd appreciate some help.
I'm pretty sure I'm almost there but I can't work out a way for it all to work together!
location / {
try_files $uri $uri/ #extensionless-php;
}
location #redirector {
rewrite ^(.*)$ /redirection-check/?request=$request_uri;
}
location #extensionless-php {
rewrite ^(.*?)/?$ $1.php;
}
location ~ ^([^.\?]*[^/])$ {
try_files $uri #addslash;
}
location #addslash {
return 301 $uri/;
}
location ~ \.php$ {
try_files $uri $uri/ #redirector;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php5.6-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
The way it should work is...
If someone lands on domain.com/page or domain.com/page.php then 301 to domain.com/page/
Interpret domain.com/page/ as domain.com/page.php
If domain.com/page.php doesn't exist then go to domain.com/redirection-check/?request=the404request. This is a 301/404 handler with a BUNCH of old urls in a big php array so they're not all listed in nginx config files.
You have a location ~ \.php$ block to process URIs that end with .php. This will handle both internally rewritten URIs (as generated by your #extensionless-php block) and externally presented URIs (such as in your 1st requirement).
To handle externally presented URIs differently, you need to look at the original request which is available as $request_uri.
The $request_uri contains the original request including the optional query string, and can be tested using a map or if directive.
For example:
if ($request_uri ~ ^(.*)\.php(\?|$)) { return 301 $1/; }
See this caution on the use of if.
I have spent muy tiempo on this and I can't get a solution.
I need a way to point the same url but with different root directory to the same application. I don't want to use rewrites because the root directoy name has to be processed by the application and rewrite discards it.
http://www.host.com/a/ --> /var/www/html/project/
http://www.host.com/b/ --> /var/www/html/project/
http://www.host.com/c/ --> /var/www/html/project/
I've managed to configure in nginx an alias directory with a fixed name vdirectory and make nginx forward the request to fastcgi.
location /vdirectory {
alias /var/www/html/project/;
try_files $uri $uri/ /vdirectory/index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
The above works OK.
Since I don't want to use fixed root directory names but arbitrary ones, this is what I've managed so far:
location ~ ^/(.?)$ {
alias /var/www/html/project/;
try_files $uri $uri/ /$1/index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Needless to say, it returns 404. It is a good sign because it means regex has recognized the path, but the request isn't forwarded correctly.
The only difference between the working config and the wrong one is /vdirectory vs ~ ^/(.?)$
alias works differently when inside a regular expression location block. See this document for details. You will need to capture the remainder of the URI and append that to the alias value. For example:
location ~ ^/(?<prefix>[^/]+)/(?<name>.*)$ {
alias /var/www/html/project/$name;
if (!-e $request_filename) { rewrite ^ /$prefix/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
I avoid using try_files and alias within the same block due to long term issues. See this caution on the use of if.
Let's say I have a nginx configuration set up for a domain like this:
server {
root /path/to/one;
server_name one.example.org;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Now, if I want to add another domain with different content, is there a way I can re-use equivalent statements from the previous domain, or do I have to duplicate everything for every new domain I want to support?
server {
root /path/to/two; # different
server_name two.example.org; # different
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I tried moving the location directive outside the server closure, but obviously things don't work like that because I got an error "location directive is not allowed here" when restarting nginx.
This is a good example to use nginx Map module. http://wiki.nginx.org/HttpMapModule
Following is what I tried. It works in my devbox. Note
map directive can only be put in the http block.
performance penalty of declaring a map directive is negligible (see above link)
you can have freedom to have different root folder, or port number, etc.
map $subdomain $root_folder {
one /path/to/one;
two /path/to/two;
}
map $subdomain $port_number {
one 9000;
two 9100;
}
server {
listen 80;
server_name ~^(?P<subdomain>.+?)\.mydomain\.com$;
root $root_folder;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:$port_number;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
you can do:
server_name one.example.org two.example.org;
if both are exactly identical except for the domainname
if you have just similar locationblocks you can move those locations to a separate file and then do an
include /etc/nginx/your-filename;
to easily use it in each serverblock