Nginx PHP-FPM alias path - php

I'm really giving up on this. I have nginx+PHP-FPM, just trying to alias a path.
Desired:
https://example.com/api/v1.0/ -> /my/folder/v1.0/
I've tried alias:
server {
server_name mysite.com;
index index.php;
location /api/v1.0/ {
index index.php;
alias /my/folder/v1.0/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
Nope. The logs reports that nginx is trying to access:
/etc/nginx/html/api/v1.0
So i changed the root:
server {
server_name mysite.com;
root /my/folder/v1.0/;
index index.php;
location /api/v1.0/ {
index index.php;
alias /my/folder/v1.0/;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
Nope. Now the alias is ignored and nginx trying to access:
/my/folder/v1.0/api/v1.0
I'm out of ideas, could you please help me?

Ok I was able to reproduce and fix it locally.
Here's what I ended up with:
# where the actual API is
root /1int/code/test/nginx;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# This basically drops /api/1.0 prefix from the URL
location ^~ /api/1.0 {
rewrite ^/api/1.0/(.*)$ /$1 last;
}
With this config, when I access /api/1.0/api.php it fetches <root>/api.php

Related

nginx: how to serve /index.php of subfolders?

I have a legacy php websiste that has been migrated to a new server with nginx (and php 7.4)
My nginx has near only this
server {
listen 80;
server_name domain.tld;
root /var/www/domain.tld;
error_log /var/log/nginx/domain.tld-error.log warn;
access_log /var/log/nginx/domain.tldt-access.log combined;
client_body_buffer_size 10M;
client_max_body_size 10M;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
If I access to mydomain.tld it works, it serves correctly mydomain.tld/index.php
but If I access mydomain.tld/subfolder or mydomain.tld/subfolder/ it doesn't serve mydomain.tld/subfolder/index.php as I expect
What am I doing wrong?
I resolved modifiying the suggestion of Daniel W
I moved the location / block under location ~ \.php$
But instead of remove the initial / I prepended also $uri
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location / {
index index.php;
try_files $uri $uri/index.php?$args;
}
If I will find some pitfalls I will update my answer

nginx $document_root with subdirectories insert the subdirectory in its path

I'm working with subdirectories. I want "babylon/webmail" to go to my rainloop webmail client.
location ^~ /webmail {
root /srv/rainloop/public_html;
try_files $uri $uri/ /webmail/index.php?$query_string;
access_log /srv/rainloop/logs/access.log;
error_log /srv/rainloop/logs/error.log;
index index.php;
access_log /var/log/nginx/scripts.log scripts;
location ~ \.php$ {
#fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_keep_conn on;
#include /etc/nginx/fastcgi_params;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
#include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ^~ /webmail/data {
deny all;
}
}
However this
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
doesn't work at all. It prints out: /srv/rainloop/public_html/webmail/index.php; That file doesn't exist in the directory structure, but: /srv/rainloop/public_html/index.php
fastcgi_param SCRIPT_FILENAME /srv/rainloop/public_html/index.php;
P.S.: After hardcoding, I don't get any error at all, but the page is blank with some rainloop code source code.
The path to the file is calculated by concatenating the value of root to the URI. The URI contains /webmail/index.php, otherwise it would not match the location block.
You probably mean to use alias instead of root as that directive removes the value of the prefix location when calculating the path to the file. See this document for details.
location ^~ /webmail {
alias /srv/rainloop/public_html;
if (!-e $request_filename) { rewrite ^ /webmail/index.php last; }
...
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Avoid using try_files and alias in the same block, due to this long term issue, and see this caution on the use of if. Use $request_filename for the SCRIPT_FILENAME value.

nginx 403 forbidden error + mac + laravel

Here is my nginx's content.
My current access url is http://localhost/lampi/, and I am receiving response 403 forbidden.
My server's documentroot is /Library/WebServer/Documents/.
When I access http://localhost/, it shows ok. I can also see the index.html page's content.
I don't know what the matter is. I have checked top 10 pages in stackoverflow.
server {
server_name localhost;
access_log /var/log/nginx/nginx.host.access.log main;
root /Library/WebServer/Documents/;
location / {
#root html;
index index.html index.htm index.php;
}
location /lampi {
#autoindex on;
if (!-e $request_filename){
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
}
location ~ \.php$ {
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /Library/WebServer/Documents/lampi/$fastcgi_script_name;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
#location /images/ {
# root /usr/local/var/www;
#}
}
There are three potential problems with your configuration file.
The use of if (!-e $request_filename) is causing problems because it checks for the existence of directories, and you should probably be using try_files anyway (see this document for details):
location /lampi {
try_files $uri $uri/ #lampi;
}
location #lampi {
rewrite ^/lampi/(.*)$ /lampi/index.php?s=$1 last;
}
The value of SCRIPT_FILENAME adds an extra /lampi into the pathname. Use either of (both evaluate to the same value in this case):
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
For example:
location ~ \.php$ {
try_files $uri =404;
include /usr/local/etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
The location ~ [^/]\.php(/|$) block is confusing. Normally it is adequate to use either location ~ \.php$ or something like location ~ [^/]\.php(/|$) depending on whether or not your application uses PATH_INFO. Delete the block you are not using. See this document for details.

Windows Nginx Inuput File not specified error

PROBLEM: Problem i am facing is when I open localhost/phpmyadmin it gives me "No Input file Specified"
I am trying to configure Nginx and php on Windows.
Nginx installed path is E:\server\nginx
php installed path is E:\server\php
PhpMyAdmin installed path is E:\phpmyadmin\
My Root directory path is E:\server\www
Following code is from nginx.conf file.
server {
listen 80;
server_name localhost;
root E:\server\www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /phpmyadmin/ {
alias E:/server/phpmyadmin/;
try_files $uri /phpmyadmin/index.php =404;
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
I am running nginx and php from command prompt
cd `E:\server\nginx\
nginx.exe`
cd E:\server\php\
php-cgi.exe -b 127.0.0.1
server runs fine. I can access localhost/
I can also access any .php file from www folder.
PROBLEM: Problem I am facing is when I open localhost/phpmyadmin it gives me "No Input file Specified"
Please tell me I am doing wrong. Thank you in advance.
Give this an attempt:
server {
listen 80;
server_name localhost;
root E:\server\www;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /phpmyadmin {
alias E:/server/phpmyadmin/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
}

nginx change phpmyadmin folder name change error

i have my phpmyadmin setup as such
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
i am looking to change the folder name so that i can access phpmyadmin through /secure
location /secure {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/secure/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/secure/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
but it keeps giving me 404 not found , any help would be appretiated guys thanks
The below works and tested
location /pma/ {
alias /usr/share/phpmyadmin/;
}
location ~ ^/pma/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
# From fastcgi_params
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT /usr/share/phpmyadmin;
}
The key is to set the below
fastcgi_param DOCUMENT_ROOT /usr/share/phpmyadmin;
this is a variable that gets se in fastcgi_params but when to below it works like a charm
change the 'pma' in both places to anything to u want and it'll work...no need for sym link
cheers
Something like this should work.
location /secure/ {
alias /usr/share/phpmyadmin/;
location ~ ^/secure/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
Try this:
location /secure {
alias /usr/share/phpmyadmin;
index index.php index.html index.htm;
location ~ ^/secure/(.+\.php)$ {
alias /usr/share/phpmyadmin/$1;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$1;
fastcgi_pass php;
}
location ~* ^/secure/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
alias /usr/share/phpmyadmin/$1;
}
}
You need to use alias in this situation.
location /secure/ {
alias /usr/share/phpmyadmin/;
With above settings all the requests to /secure/ will be dropped to /usr/share/phpmyadmin/.

Categories