Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了。
下面我就来说说server_name的使用吧:
server_name的匹配顺序
Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:
1、准确的server_name匹配,例如:
|
1
|
|
|
1
2
3
4
5
|
server {listen 80;server_name ssdr.info www.ssdr.info;...} |
2、以*通配符开始的字符串:
|
1
|
|
|
1
2
3
4
5
|
server {listen 80;server_name *.ssdr.info;...} |
3、以*通配符结束的字符串:
|
1
|
|
|
1
2
3
4
5
|
server {listen 80;server_name www.*;...} |
4、匹配正则表达式:
|
1
|
|
|
1
2
3
4
5
|
server {listen 80;server_name ~^(?.+)\.howtocn\.org$;...} |
Nginx将按照1,2,3,4的顺序对server name进行匹配,只有有一项匹配以后就会停止搜索,所以我们在使用这个指令的时候一定要分清楚它的匹配顺序(类似于location指令)。
|
1
|
|
|
1
2
3
4
5
6
7
|
server{listen 80;server_name ~^(www\.)?(.+)$;index index.php index.html;root /data/wwwsite/$2;} |
站点的主目录应该类似于这样的结构:
|
1
|
|
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
|
server{listen 80;server_name ~^(.+)?\.howtocn\.org$;index index.html;if ($host = ssdr.info){rewrite ^ http://www.ssdr.info permanent;}root /data/wwwsite/ssdr.info/$1/;} |
站点的目录结构应该如下:
|
1
|
|
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server{listen 80;server_name ~^(.+)?\.howtocn\.org$;set $www_root $1;root /data/wwwsite/ssdr.info/$www_root/;location ~ .*\.php?$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME /data/wwwsite/ssdr.info/$fastcgi_script_name;include fastcgi_params;}} |
Nginx不同域名反向代理到另一台服务器 proxy_pass和$host
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server {listen 80;server_name $host;location / {proxy_pass http://www.31.gd/;proxy_set_header Host $host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 60;proxy_read_timeout 600;proxy_send_timeout 600;} |
下面的一并修改吧。
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
location /.(php|php5)?${fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;include fcgi.conf;}location /status {stub_status on;access_log off;}location /.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;}location /.(js|css)?${expires 12h;} |
这样就可以实现了前端VPS可以反向代理任意域名到后端VPS,只要将域名解析到前端VPS,后端VPS进行域名绑定,那么就可以直接访问到了
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
## A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81;server_name *.efg.com;location / {proxy_pass http://localhost:8080;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}## A virtual host using mix of IP-, name-, and port-based configuration#server {listen 81;server_name *.hij.com;location / {proxy_pass http://localhost:8081;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}} |
泛域名解析关键为红色部分,如果没有红色部分,后端8080及8081口对应的tomcat虚拟主机将无法获得域名信息,导致后端tomcat无法获取到对应的域名信息。
|
1
|
|
|
1
2
3
4
5
6
7
8
|
server{listen 80;server_name www.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/web126.com; #网站根目录include location.conf; #调用其他规则,也可去除} |
然后重起nginx服务器,域名就绑定成功了。
|
1
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
server{listen 80;server_name www.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/web126.com; #网站根目录include location.conf; #调用其他规则,也可去除}server{listen 80;server_name msn.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/msn.web126.com; #网站根目录include location.conf; #调用其他规则,也可去除} |
三、不带www的域名加301跳转
|
1
|
|
|
1
2
3
4
5
6
|
server{listen 80;server_name web126.com;rewrite ^/(.*) http://www.web126.com/$1 permanent;} |
四、添加404网页
|
1
|
|
|
1
2
3
4
5
6
7
8
9
|
server{listen 80;server_name www.web126.com; #绑定域名index index.htm index.html index.php; #默认文件root /home/www/web126.com; #网站根目录include location.conf; #调用其他规则,也可去除error_page 404 /404.html;} |
最后还有一个方法需要注意,可能有需要禁止IP直接访问80端口或者禁止非本站的域名绑定我们的IP,这样的话应该
|
1
|
|
|
1
2
3
4
5
|
server{listen 80 default;server_name _;return 403;} |
学会上面四种规则方法,基本就可以自己独立解决nginx 多域名配置问题了。
原文:http://www.cnblogs.com/1995hxt/p/5424176.html