Answer the question
In order to leave comments, you need to log in
What is the problem of config for subdomain in nginx
Good afternoon!
There is a config:
server {
listen 80;
listen [::]:80 default ipv6only=on;
server_name my.com ~^([.+])\.my.com$;
set $subdomain $1;
access_log /var/log/nginx/localhost.access.log;
error_log /var/log/nginx/localhost.error.log info;
root /var/www/$subdomain;
location / {
index index.html index.htm;
}
location ~* \.(jpg|ico|gif|png|css|js|svg)$ {
expires 30d;
}
}
Answer the question
In order to leave comments, you need to log in
The problem is with the regular expression. Should be something like ([a-z0-9-\.]+), not ([.+]). The current version means " any one character and plus".
nginx.org/en/docs/http/server_names.html
nginx.org/en/docs/http/converting_rewrite_rules.html
wiki.nginx.org/IfIsEvil
wiki.nginx.org/Pitfalls
man pcresyntax
The correct config would look like this:
server {
listen 80;
server_name www.my.com;
return 301 http://my.com$request_uri;
}
server {
listen 80;
server_name ~^(?<sd>.+)\.my\.com$;
return 301 http://my.com/$sd$request_uri;
}
server {
listen 80; ## listen for ipv4
listen [::]:80 default ipv6only=on; ## listen for ipv6
server_name my.com;
access_log /var/log/nginx/artzub.access.log main;
error_log /var/log/nginx/artzub.error.log info;
root /var/www;
location / {
index index.html index.htm;
}
location ~* \.(?:jpg|ico|gif|png|css|js|svg)$ {
access_log off;
expires 30d;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question