using alias to send certain URL to different php directory - nginx

I am currently sending all request with '.php' to the php engine using the following:
root /var/www/public;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
I would like to get all requests to /login/... to be sent to a different directory on the php server. I gather that alias and try_files don't work together. I have tried using another 'root', but it doesn't work:
location ^~ /login/ {
root /var/www/login/etc/;
try_files $uri $uri/ /login$is_args$args;
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
How can I get this to work so all /login/... requests end up being served from /var/www/login/etc directory on php. Thanks.

Try this,
http {
...
map $request_uri $rot{
"~ /login" /var/www/login/etc/;
default <insert_default_root>;
}
server {
...
root $rot;
...
location / {...}
location ~ \.php$ {...}
...
}
...
}

Related

How to configure nginx for api and front

Please tell me how to configure nginx so that it serves a file
/var/www/public/frontend/index.html
for all requests except
/api/
For api should give
/var/www/public/index.php
Tried using
server {
listen 80;
server_name localhost;
root /var/www/public;
location /api/ {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
}
server {
listen 80;
server_name localhost;
root /var/www/public/frontend;
location / {
try_files $uri $uri/ /index.html?$query_string;
}
}
and
server {
listen 80;
server_name localhost;
root /var/www/public;
location /api/ {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
try_files $uri $uri/ /frontend/index.html?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
}
But it doesn't work. Either one or the other works. I know that it would be possible to make a subdomain, but at the moment I need to do without it
The first one definitely won't work, you should use only one server block for this case. For the second one, you cantry either
location / {
try_files /frontend$uri /frontend$uri/ /frontend/index.html?$query_string;
}
or
location / {
root /var/www/public/frontend;
try_files $uri $uri/ /index.html?$query_string;
}

Nginx try_files index.php fails to get processed

I have this config setup:
location / {
#try_files $uri /index.php$is_args$args; <-- works as expected
#try_files $uri /index.php$is_args$args /maintanence.html; <-- does not work
#try_files $uri /maintanence.html; < -- switches to maintenance page as expected
try_files $uri /index.php /maintanence.html; <-- also does not work
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
1st and 3rd lines work 2nd and last lines do not.
I have noticed that whenever you put any other filename after index.php it just returns raw index.php contents in response.
Can someone explain why this happens?
Your comment states that you wish to deliver maintenance.html if the file index.php does not exist.
The try_files statement that checks for the existence of index,php must be in the same location that processes the request.
You could do it in the location ~ \.php$ block if you want the non-existence of any .php file to trigger maintenance.html, but the solution below will limit the check to index.php only.
For example:
location / {
try_files $uri /index.php$is_args$args;
}
location = /index.php {
try_files $uri /maintanence.html;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass unix:/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Why my nginx config doesn't work?

I'm trying to setup nginx to begin working with phalcon, here's the config :
server {
listen 80;
server_name localhost.dev;
set $root_path '/usr/share/nginx/html/phalcon/public';
root $root_path;
index index.php index.html index.htm;
charset utf-8;
rewrite_log on;
location / {
try_files $uri $uri/ /index.php?_url=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index /index.php;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path/public;
}
location ~ /\.ht {
deny all;
}
}
I tried all possibilites but still I can't get it working.
Any clues?
look here
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}

Ngnix downloading php

I am trying to get nginx to route all requests starting with /embed to /home/forge/dev.tline.io/embed/index.php
My Nginx config:
location /embed {
root /home/forge/dev.tline.io;
try_files /embed/index.php =404;
}
location / {
root /home/forge/dev.tline.io;
index index.html index.htm;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
All requests go to /embed/index.php but it doesn't run the php file it downloads it.
Note: http://dev.tline.io/embed/index.php is compiled not downloaded
I got it to work if add
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
into location /embed but there should be a better way to do this
This should execute /embed/index.php for all /embed URLs:
server {
root /home/forge/dev.tline.io;
location / {
index index.html index.htm;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
}
location /embed {
fastcgi_param SCRIPT_NAME $document_root/embed/index.php;
fastcgi_param SCRIPT_FILENAME $document_root/embed/index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include fastcgi_params;
}
}
Please try out the following code,
map $request_uri $rot {
"~ /embed" /home/forge/dev.tline.io/embed/;
default /home/forge/dev.tline.io/;
}
map $request_uri $ind {
"~ /embed" index.php;
default index.html;
}
server {
...
root $rot;
index index.php index.html index.htm;
...
location / {
try_files $uri$args $uri$args/ $uri $uri/ /$ind =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
...
}
If this doesn't work out, try switching $ind position suitably, and check error log in case of extra '/' found.

nginx configuration for Laravel 4

I am trying to setup my Laravel 4 project using nginx . Here is my nginx server block for laravel :
server {
listen 80;
root /home/prism/www/laravel/public;
index index.php index.html index.htm;
server_name example.com;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
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;
}
But my problem is , Its showing "404 not found" error for all other routes except the default one , that comes with default installation .
This is an NGINX Configuration i've used with Laravel 4 and Laravel 4.1 that works.
server {
listen 80;
server_name sub.domain.com;
set $root_path '/var/www/html/application_name/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}
you could try this for location / { ... }
location / {
try_files $uri $uri/ /index.php?$query_string;
}
$query_string worked for me.

Resources