Sunday, June 5, 2016

Nginx: rewrite for subdomain to folder AND files inside it



I have nginx rewrite rule - it redirects all subdomain requests from sub to folder:



server {
listen 77.77.77.77:80;
server_name domaincom *.domaincom;

root /home/admin/web/domaincom/public_html/subs/$subdomain;

set $subdomain "";
if ($host ~* ^([a-z0-9-\.]+)\.domaincom$) {
set $subdomain $1;
#rewrite $request_uri $1.$request_uri;
#rewrite ^(.*txt)$ $subdomain.$request_uri; just testing here
#rewrite ^(.*\.txt)$ $subdomain.$request_uri/$1;

}
if ($host ~* ^www.domaincom$) {
set $subdomain "";

}

# location $subdomain {
# # # An example rewrite
# rewrite ^/(.*txt)$ $request_uri;
# }

index index.php;
access_log off;
error_log /dev/null;



location / {

root /home/admin/web/domaincom/public_html;
location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ {
expires max;
}

location ~ [^/]\.php(/|$) {

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}

fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}


error_page 403 /error/404.html;
error_page 404 = @foobar;

location @foobar {
return 301 /;
}
error_page 500 502 503 504 /error/50x.html;

location /error/ {

alias /home/admin/web/domaincom/document_errors/;
}

# Access deny for bots
# See bots list in the /etc/nginx/nginx.conf
if ($limit_bots = 1) {
return 403;
}



location ~* "/\.(htaccess|htpasswd)$" {
deny all;
return 404;
}

location /vstats/ {
alias /home/admin/web/domaincom/stats/;
include /home/admin/conf/web/domaincom.auth*;
}


include /etc/nginx/conf.d/phpmyadmin.inc*;
include /etc/nginx/conf.d/phppgadmin.inc*;
include /etc/nginx/conf.d/webmail.inc*;

include /home/admin/conf/web/nginx.domaincom.conf*;
}


It work well, but i have one problem.




And in domain and subdomains i have a same php-script that gets data from txt file, like this:



file(key.txt);


My php-fpm module, i think, doesn't know about nginx rules and gets data in SUB from ROOT domain - it's wrong.
Please help me to add nginx exceptions or add rule to get txt's data in SUB from SUBs. Not_from_root_domain. Thanks.


Answer



Some optimizations of your config:




1) Defining your subdomain root with the help of map directive:



map $http_host $root_suffix {
~^(?:www\.)?domain\.com$ "";
~^(.+)\.domain\.com$ /subs/$1;
}


2) Defining expire time based on request URI suffix:




map $uri $expires {
default off;
~*.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ max;
}


(This map directives must be defined outside the server { } block, at the http { } context level.)



Now your server { } block:




server {
listen 77.77.77.77:80;
server_name .domaincom;
root /home/admin/web/domaincom/public_html$root_suffix;
expires $expires;

index index.php;
access_log off;
error_log /dev/null;


# your locations here
location ~ [^/]\.php(/|$) {
...
}
...

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...