I have this directory structure for my projects
/var/www
/project-a
/data <-- configuration and user files/data
/app <-- all the code is in sub-dirs in here
/pm <-- a home brew project management app
/.pmdata <-- data used by pm
My goal is to configure NGINX so I can access the project itself through
http://project-a.dev/ and the project management with http://project-a.dev/pm/.
In other words, I want the second url preserved as is, but if the url does not point to /pm/* it should be re-written to have the missing /app prepended to it.
I have tried the following configuration but http://project-a.dev/pm/ results in 404 and http://project-a.dev/ first redirects to http://project-a.dev/app/ and then gives 404.
What am I doing wrong?
server {
listen 127.0.0.1:80;
root /var/www/project-a;
index index.php index.html index.htm;
server_name project-a.dev;
location / {
try_files $uri $uri/app $uri/app/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Alternatively you could append /app to the root value for all URIs that do not begin with /pm. For example:
server {
...
root /var/www/project-a/app;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
location ^~ /pm {
root /var/www/project-a;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
}
The nested location block for location ~ \.php$ executes PHP files within the /pm hierarchy. The ^~ modifier is necessary to avoid the other location ~ \.php$ block taking control. See this document for details.
Related
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;
}
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.
I am really new to Nginx, and am to add a new website to a remote Nginx server.
I notice that /etc/nginx/conf.d/default.conf has been modified to this:
server {
server_name api.example.com;
root /var/www/html/api/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want to add another url: testapi.example.com to the same server. All the tuturiols point to /etc/nginx/sites-enabled/, but that folder is empty. This is a production machine, so I would not like bring any downtime.
Please help.
You could add another server block to /etc/nginx/conf.d/default.conf for your second domain or a better way would be to create a new .conf file in /etc/nginx/conf.d/ (with a meaningful name) and in that file define a new server block for the second domain.
So you'd keep your /etc/nginx/conf.d/default.conf
server {
server_name api.example.com;
root /var/www/html/api/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
And add /etc/nginx/conf.d/testapi.conf
server {
server_name testapi.example.com;
root /var/www/html/testapi/root;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
... rest of config for second domain...
}
I have config(minimal):
server {
listen test.local:80;
server_name test.local;
server_name_in_redirect off;
location / {
root /data/www/test/public;
try_files $uri $uri/ /index.php?route=$uri&$args;
index index.html index.php;
}
location ~ \.php$ {
root /data/www/test/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www/test/public$fastcgi_script_name;
fastcgi_param ...
...
}
}
Thats work file for urls /help/ or /contacts/ etc… (all redirect to index.php with get variables).
But if url, for example, /help.php or contacts.php, and this files not exists, I have output:
File not found.
How update my Nginx config? I need URLs, for example:
/help.php => /index.php?route=/help.php
/contacts.php?foo=bar... => /index.php?route=/contacts.php&args=…
Here is simple config for 404 error page . Its custom 404 page.
error_page 404 /404.php;
location /404.php {
root /var/www/public_html;
allow all;
}
Last part
Important line is ========== try_files $uri =404;
location ~ .php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/public_html$fastcgi_script_name;
include fastcgi_params;
}
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.