一个server模块配置多个虚拟站点
2015-11-20 09:39
453 查看
一个server模块配置多个虚拟站点Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为:
(1)、准确的server_name匹配,例如:
server_name www.jason.com;
(2)、以*通配符开始的字符串:
server_name *.jason.com;
(3)、以*通配符结束的字符串:
server_name www.*;
(4)、匹配正则表达式:
server_name ~^(?.+)\.jason\.com$;
Nginx将按照1,2,3,4的顺序对servername进行匹配,只要有一项匹配成功后就会停止搜索。官方解释如下:
During searching for a virtual server byname, if the name matches more than one of the specified variants, (e.g. both awildcard name and regular expression match), the first matching variant will bechosen, in the following order of priority:
the exact name
the longest wildcard name starting with an asterisk, e.g. “*.example.com”
the longest wildcard name ending with an asterisk, e.g. “mail.*”
the first matching regular expression (in order of appearance in the configuration file)
在一个server块中配置多个站点。
1、配置nginx
user nobody;
worker_processes 1;
pid nginx.pid;
error_log logs/error.log notice;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent""$http_x_forwarded_for"';
log_format download '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
client_max_body_size 20M;
client_header_buffer_size 4k;
large_client_header_buffers 4 8k;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_buffers 4 8k;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
server {
listen80;
server_name~^(www\.)?(.+)$;
access_loglogs/access.log main;
server_name_in_redirect off;
location / {
indexindex.html;
root/usr/local/nginx/html/$2;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
location /status {
stub_status on;
access_log/usr/local/nginx/logs/status.log;
auth_basic"NginxStatus";
auth_basic_user_file/usr/local/nginx/conf/htpasswd;}
location ~ /\.ht {
deny all;
}
}
}
备注:红色字体为多个站点配置。
2、 页面文件目录创建,站点的主目录应该类似于这样的结构:
/usr/local/nginx/html/test.org
/usr/local/nginx/html/test.com
/usr/local/nginx/html/Jason.org
如下创建
[root@jason1 html]# mkdir test.com test.orgjason.org
[root@jason1 html]# cd test.com/
[root@jason1 test.com]# ls
[root@jason1 test.com]# echo "this is test.com" >>index.html
[root@jason1 test.com]# cd ..
[root@jason1 html]# cd test.org
[root@jason1 test.org]# echo "this istest.org">> index.html
[root@jason1 test.org]# cd ..
[root@jason1 html]# cd jason.org/
[root@jason1 jason.org]# echo "this isjason.org" >> index.html
[root@jason1 jason.org]#
3、启动nginx,设置域名与ip记录,进行访问测试。
(1)、准确的server_name匹配,例如:
server_name www.jason.com;
(2)、以*通配符开始的字符串:
server_name *.jason.com;
(3)、以*通配符结束的字符串:
server_name www.*;
(4)、匹配正则表达式:
server_name ~^(?.+)\.jason\.com$;
Nginx将按照1,2,3,4的顺序对servername进行匹配,只要有一项匹配成功后就会停止搜索。官方解释如下:
During searching for a virtual server byname, if the name matches more than one of the specified variants, (e.g. both awildcard name and regular expression match), the first matching variant will bechosen, in the following order of priority:
the exact name
the longest wildcard name starting with an asterisk, e.g. “*.example.com”
the longest wildcard name ending with an asterisk, e.g. “mail.*”
the first matching regular expression (in order of appearance in the configuration file)
在一个server块中配置多个站点。
1、配置nginx
user nobody;
worker_processes 1;
pid nginx.pid;
error_log logs/error.log notice;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent""$http_x_forwarded_for"';
log_format download '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
client_max_body_size 20M;
client_header_buffer_size 4k;
large_client_header_buffers 4 8k;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
gzip on;
gzip_min_length 1k;
gzip_http_version 1.1;
gzip_buffers 4 8k;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
server {
listen80;
server_name~^(www\.)?(.+)$;
access_loglogs/access.log main;
server_name_in_redirect off;
location / {
indexindex.html;
root/usr/local/nginx/html/$2;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
location /status {
stub_status on;
access_log/usr/local/nginx/logs/status.log;
auth_basic"NginxStatus";
auth_basic_user_file/usr/local/nginx/conf/htpasswd;}
location ~ /\.ht {
deny all;
}
}
}
备注:红色字体为多个站点配置。
2、 页面文件目录创建,站点的主目录应该类似于这样的结构:
/usr/local/nginx/html/test.org
/usr/local/nginx/html/test.com
/usr/local/nginx/html/Jason.org
如下创建
[root@jason1 html]# mkdir test.com test.orgjason.org
[root@jason1 html]# cd test.com/
[root@jason1 test.com]# ls
[root@jason1 test.com]# echo "this is test.com" >>index.html
[root@jason1 test.com]# cd ..
[root@jason1 html]# cd test.org
[root@jason1 test.org]# echo "this istest.org">> index.html
[root@jason1 test.org]# cd ..
[root@jason1 html]# cd jason.org/
[root@jason1 jason.org]# echo "this isjason.org" >> index.html
[root@jason1 jason.org]#
3、启动nginx,设置域名与ip记录,进行访问测试。
相关文章推荐
- Seafile Server本地权限提升漏洞(CVE-2014-5443)
- 从USB安装Ubuntu Server 10.04.3 图文详解
- 使用zabbix监控Nginx活动状态--Part1
- 高效访问Internet-启用ISA Server的缓存
- windows server域用户提升到本地更高权限组中的方法
- 完美解决SQL Server 安装问题:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- IIS 错误 Server Application Error 详细解决方法
- 在WINXP系统上安装SQL Server企业版的方法
- SQL Server 存储过程的分页
- SQL Server复制需要有实际的服务器名称才能连接到服务器
- SQL Server 2000向SQL Server 2008 R2推送数据图文教程
- SQL server 表操作介绍
- SQL Server 2000 注入防护大全(二)
- SQL Server 2000 注入防护大全(一)
- 怎样把Windows server 2003转换成工作站系统
- Windows Server 2007四月CTP发布 下载
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法
- Microsoft SQL Server 2008安装图解教程(Windows 7)
- ASP基础入门第十篇(ASP内建对象Server)
- ISA Server 的故障排除工具(2)