Reuse configuration statements for domains in nginx.conf - nginx

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

Related

NGINX location giving 404 error, even though the directory exists [duplicate]

lets say I've a path like:
/var/www/myside/
that path contains two folders... let's say
/static and /manage
I'd like to configure nginx to have an access to:
/static folder on / (eg. http://example.org/)
this folder has some .html files.
/manage folder on /manage (eg. http://example.org/manage) in this case this folder contains Slim's PHP framework code - that means the index.php file is in public subfolder (eg. /var/www/mysite/manage/public/index.php)
I've tried a lot of combinations such as
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite;
location /manage {
root $uri/manage/public;
try_files $uri /index.php$is_args$args;
}
location / {
root $uri/static/;
index index.html;
}
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
}
}
The / works correctly anyway manage doesn't. Am I doing something wrong? Does anybody know what should I change?
Matthew.
To access a path like /var/www/mysite/manage/public with a URI like /manage, you will need to use alias rather than root. See this document for details.
I am assuming that you need to run PHP from both roots, in which case you will need two location ~ \.php blocks, see example below. If you have no PHP within /var/www/mysite/static, you can delete the unused location block.
For example:
server {
listen 80;
server_name example.org;
error_log /usr/local/etc/nginx/logs/mysite/error.log;
access_log /usr/local/etc/nginx/logs/mysite/access.log;
root /var/www/mysite/static;
index index.html;
location / {
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /manage {
alias /var/www/mysite/manage/public;
index index.php;
if (!-e $request_filename) { rewrite ^ /manage/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
}
The ^~ modifier causes the prefix location to take precedence over regular expression locations at the same level. See this document for details.
The alias and try_files directives are not together due to this long standing bug.
Be aware of this caution in the use of the if directive.

nginx aliases and dynamic directory names

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.

Nginx multiple locations each with its own root

server {
listen 80;
server_name ~^(?<cc>.+?).local.solar.bc.digital$;
client_max_body_size 1m;
root /home/vagrant/sites/$cc/_www/;
index index.html index.htm index.php;
error_page 404 /index.php;
access_log /var/log/nginx/$cc-access.log;
error_log /var/log/nginx/$cc-error.log;
charset utf-8;
sendfile off;
location / {
root /home/vagrant/sites/$cc/_www/php/;
try_files $uri $uri/ /index.php?$query_string;
}
location /shop/ {
# root /home/vagrant/sites/$cc/_www/bcshop/;
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param APP_ENV dev;
fastcgi_param PLATFORM_ENVIRONMENT local;
fastcgi_read_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location ~ /\.ht {
deny all;
}
}
Right, given the config above. If you see what I am trying to do, know that it's not working!
Some details:
I have the general 'root', because without I get errors.
This is on a local VM used for development. We have a deployment tool locally that for each project (refer to $cc variable in the config above), it can deploy up to two apps. Not necessarily both are deployed for each project. The first app is deployed in /home/vagrant/sites/$cc/_www/php, the second one in /home/vagrant/sites/$cc/_www/bcshop (but I can make it shop - you'll see why from the config)
Both apps run on Drupal, so the way the load is the same. I simply can't make them work. I get file not found for both with most of the small variants I've tried.
When I place the second app in /home/vagrant/sites/$cc/_www/shop (rather than /home/vagrant/sites/$cc/_www/bcshop) I can get its homepage to load, but nothing else.
When I disregard the second app, and concentrate on the first, I can simply have a general root set to /home/vagrant/sites/$cc/_www/php and remove the local root from location /. That works. But it doesn't when it's specified as local root. In this case, in fact, I get this error in the logs: "FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream". This only for the first app.
If you wonder why the general root is set to that value, it's because I get an error in the logs if the general root is not verifiable for the location being matched. Makes sense?
I think I've covered pretty much all of what I've done in summary.
Thoughts?
I am not an expert at all with nginx, so I am going by trial and errors, with the help of the docs and other questions answered on here. But so far, no joy.
Thanks all.
You have PHP files in both the _www/bcshop/ path and the _www/php/ path. If you would like to use one common location ~ \.php$ for both applications, then the URI for each application will need to be prefixed by /bcshop and /php respectively. That is, both applications appear to run in a subdirectory.
I suspect that you would like to use /shop prefix for one application, and the / prefix for the other. in which case two location ~ \.php$ blocks will be required.
One application runs from the server root:
root /home/vagrant/sites/$cc/_www/php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
...
}
And one application runs with a URI prefix of /shop but located in the subdirectory _www/bcshop:
location ^~ /shop {
rewrite ^/shop(?:/(.*))?$ /bcshop/$1 last;
}
location ^~ /bcshop/ {
internal;
root /home/vagrant/sites/$cc/_www;
try_files $uri /shop/index.php?$query_string;
location ~ \.php$ {
try_files $uri /shop/index.php;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
...
}
}
The ^~ modifier ensures that the location block takes precedence over the regular expression location block above. See this document for details.
Although the external URI prefix is /shop, it is silently rewritten to /bcshop so that we can continue to use the root directive. There is an alias directive, but it has issues with try_files and is tricky to use with PHP.
Based on the answer by #RichardSmith, I come up with my own variant (which is working fine). Simply because repeating the block for location ~ \.php$ was doing my head in.
...
...
root /home/vagrant/sites/$cc/_www/;
...
...
location / {
set $actual_root /home/vagrant/sites/$cc/_www/php/;
set $fastcgi_index /index.php;
root $actual_root;
try_files $uri $uri/ $fastcgi_index?$query_string;
}
location /shop/ {
set $actual_root /home/vagrant/sites/$cc/_www/;
set $fastcgi_index /shop/index.php;
root $actual_root;
try_files $uri $uri/ $fastcgi_index?$query_string;
}
location ~ \.php$ {
root $actual_root;
fastcgi_index $fastcgi_index;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $actual_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param APP_ENV dev;
fastcgi_param PLATFORM_ENVIRONMENT local;
fastcgi_read_timeout 300;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
...
...

Rewrite within location with a new root/alias - Nginx

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;
}
}

nginx alias+location directive

server {
listen 80;
server_name pwta;
root html;
location /test/{
alias html/test/;
autoindex on;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
This configuration works. However, if location /test/ is replaced e.g. location /testpath/ it doesn't work (No input file specified). I assumed base on the explanation of alias directive that the "location" part is dropped and thus /testpath/info.php would result in html/test/info.php.
Thanks for any suggestion.
nginx alias
server {
listen 80;
server_name pwta;
index index.html index.php;
root html;
location /testpath/ {
alias html/test/;
}
location ~ ^/testpath/(.+\.php)$ { ### This location block was the solution
alias html/test/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$1;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Both the alias and root directives are best used with absolute paths. You can use relative paths, but they are relative to the prefix config option used to compile nginx, and are generally not what you want.
You can see this by executing nginx -V and finding the value following --prefix=.
Prove this to yourself by looking at the log, you will find a "no such file" error.

Resources