nginx 配置2个nginx域名指向为什么都指向同一个网站

Nginx服务器下配置个性二级域名及多个域名的实例讲解
作者:别闹我要练字
字体:[ ] 类型:转载 时间:
这篇文章主要介绍了Nginx服务器下配置个性二级域名及多个域名的实例讲解,注意一下rewrite的写法规则,需要的朋友可以参考下
个性二级域名
配置下个性化二级域名.
访问的URL http://custom.jb51.net
实际的URL http://www.jb51.net/auth/custom
我们的做法就是通过服务器配置,将访问的url转换为实际的url
下面我们来用nginx配置。配置如下:
server_name *.jb51.
if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
set $subdomain $1;
location / {
rewrite ^/$ /auth/$
proxy_pass http://www.jb51.net/;
其中的if,是用于过滤某些特殊的二级域名,比如www,然后获取二级域名的变量值。
rewrite转化为对应的目录
rewrite ^/$ /auth/$
再加上nginx的反向代理功能:
proxy_pass http://www.jb51.net/;
就可以实现了。
这样配置之后,除了if中过滤的二级域名,其他的二级域名{sudomain}.jb51.net,对于服务器,其真实的路径都是www.jb51.net/auth/{sudomain}。
如果url有多种路径规则,则需要一一进行配置。
多域名配置
& nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里。
一、每个域名一个文件的写法
&&&&&& 首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers ,如要绑定域名www.jb51.net 则在此目录建一个文件:www.your-domain.conf然后在此文件中写规则,如:server
server_name
index.htm index.html index. #默认文件
/home/www/jb51. #网站根目录
location. #调用其他规则,也可去除
然后重起nginx服务器,域名就绑定成功了nginx服务器重起命令:/etc/init.d/nginx restart
二、一个文件多个域名的写法
一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:server
server_name
index.htm index.html index. #默认文件
/home/www/bbs.jb51.
location. #调用其他规则,也可去除
server_name
index.htm index.html index. #默认文件
/home/www/www.jb51.
#网站根目录
location. #调用其他规则,也可去除
三、不带www的域名加301跳转
如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:
server_name
^/(.*) http://www.jb51.net/$1
四、添加404网页
&&&&&& 添加404网页,都可又直接在里面添加,如:
server_name
www.jb51. #绑定域名
index.htm index.html index. #默认文件
/home/www/jb51. #网站根目录
location. #调用其他规则,也可去除
error_page
学会上面四种规则方法,基本就可以自己独立解决nginx 多域名配置问题了
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具设置nginx绑定多个域名
nginx绑定多个域名,可把多个域名规则写一个配置文件里,也可分别建立多个域名配置文件,一般为了管理方便,建议每个域名建一个文件,有些同类域名也可写在一个总的配置文件里。
一、每个域名一个文件的写法
首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers ,如要绑定域名 则在此目录建一个文件:.conf然后在此文件中写规则,如:
listen 80;
server_ #绑定域名
index index.htm index.html index. #默认文件
root /home/; #网站根目录
include location. #调用其他规则,也可去除
然后重起nginx服务器,域名就绑定成功了nginx服务器重起命令:/etc/init.d/nginx restart
二、一个文件多个域名的写法
一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:
listen 80;
server_ #绑定域名
index index.htm index.html index. #默认文件
root /home/; #网站根目录
include location. #调用其他规则,也可去除
listen 80;
server_ #绑定域名
index index.htm index.html index. #默认文件
root /home/www/; #网站根目录
include location. #调用其他规则,也可去除
三、不带www的域名加301跳转
如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:
listen 80;
rewrite ^/(.*) /$1
四、添加404网页
添加404网页,都可又直接在里面添加,如:
listen 80;
server_ #绑定域名
index index.htm index.html index. #默认文件
root /home/; #网站根目录
include location. #调用其他规则,也可去除
error_page 404 /404.
四种规则方法,就可以自己独立解决nginx 多域名配置问题了。
配置多域名
方法一:多个的.conf方法(优点是灵活,缺点就是站点比较多配置起来麻烦)
这里以配置2个站点(2个域名)为例,n个站点可以相应增加调整,假设:
IP地址:192.168.1.100
域名放在/ www / example1
域名放在/ www / example2
配置nginx虚拟主机的基本思路和步骤如下:
把2个站点,放到nginx可以访问的目录/ www /
给每个站点分别创建一个nginx配置文件.conf,.conf,并把配置文件放到/ usr / local / nginx / vhosts /
然后在/usr/local/nginx/nginx.conf里面加一句包括进步2创建的配置文件全部包含进来(用*号)
1,打开/usr/local/nginx/nginix.conf文件,在相应位置加入包括把以上2个文件包含进来
worker_processes 1;
#主服务器错误日志
error_log /usr/local/nginx/log/nginx/error.
pid /usr/local/nginx/nginx.
worker_connections 51200;
#主服务器配置
default_type application / octet-
log_format main’$ remote_addr – $ remote_user [$ time_local] $ request’
‘“$ status”$ body_bytes_sent“$ http_referer”’
‘“$ http_user_agent”“$ http_x_forwarded_for”’;
#keepalive_timeout 0;
keepalive_timeout 65;
服务器名称 _;
access_log /usr/local/nginx/log/nginx/access.
server_name_in_redirect关闭
root / usr / share / nginx /
index index.
#包含所有的虚拟主机的配置文件
包括/ usr / local / nginx / vhosts / *;
2,在/ usr / local / nginx下创建vhosts目录
mkdir / usr / local / nginx / vhosts
3,在/ usr / local / nginx / vhosts /里创建一个名字为.conf的文件,把以下内容拷进去
server_ www。;
access_log / www / access_ example1.
index index.php index.html index.
error_page 500 502 503 504 /50x.
location = /50x.html {
root / usr / share / nginx /
#将PHP脚本传递给FastCGI服务器,侦听127.0.0.1:9000
位置?.php $ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.
fastcgi_param _FILENAME //$fastcgi__
包括fastcgi_
位置?/.ht {
4,在/ usr / local / nginx / vhosts /里创建一个名字为.conf的文件,把以下内容拷进去
server_ www。;
access_log / www / access_ example1.
index index.php index.html index.
error_page 500 502 503 504 /50x.
location = /50x.html {
root / usr / share / nginx /
#将PHP脚本传递给FastCGI服务器,侦听127.0.0.1:9000
位置?.php $ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.
fastcgi_param _FILENAME //$fastcgi__
包括fastcgi_
位置?/.ht {
5,重启Nginx
/etc/init.d/nginx重启
动态目录方法(优点是方便,每个域名对应一个文件夹,缺点是不灵活)
这个简单的方法比起为每一个域名建立一个vhost.conf配置文件来讲,只需要在现有的配置文件中增加如下内容:
根据需求改变这个端口
听80; #could也可以是1.2.3.4:80也可以是1.2.3.4:80的形式
#多个主机名由空格分隔。更换这些。
#多个主机名可以用空格隔开,当然这个信息也是需要按照你的需求而改变的
。server_name
* . http://www.*./;
#Alternately:_ *
#或者可以使用:_ *(具体内容参见本维基其他页面)
root / PATH / TO / WEBROOT / $
error_page 404 /errors/404.
access_log logs / .access.
location / {
root / PATH / TO / WEBROOT / $ host /;
index index.
#直接提供静态文件
#直接支持静态文件(从配置上看不是直接支持啊)
位置?* ^。+。(jpg | jpeg | gif | css | png | js | ico | html)$ {
位置?.php $ {
#一定要使用不同的服务器为fcgi进程,如果你需要
#如果需要,你可以为不同的FCGI进程设置不同的服务信息
fastcgi_pass 127.0.0.1:YOURFCGIPORTHERE;
fastcgi_index index.
fastcgi_param _FILENAME / PATH / TO / WEBROOT / $ host / $ fastcgi__
fastcgi_param QUERY_STRING $ query_
fastcgi_param REQUEST_METHOD $ request_
fastcgi_param CONTENT_TYPE $ content_
fastcgi_param CONTENT_LENGTH $ content_
fastcgi_intercept_
位置?/.ht {
最后附另外一个二级域名匹配的方法
server_name * .;
主机名if($ host?*(。*)。(。*)。(。*))
set $ domain $ 1;
root html / abc / $ domain /;
location /
root html / abcd / $
index index.html index.
数安时代(GDCA)致力于网络信息安全,已通过WebTrust 的国际认证,是全球可信任的证书签发机构。GDCA专业技术团队将根据用户具体情况为其提供最优的产品选择建议,并针对不同的应用或服务器要求提供专业对应的HTTPS解决方案。GDCA一直以“构建网络信任体系,服务现代数字生活”的宗旨,致力于提供全球化的数字证书认证服务。其自主品牌——信鉴易 TrustAUTH SSL证书:包括 OVSSL、EVSSL、代码签名证书等。为涉足互联网的企业打造更安全的生态环境,建立更具公信力的企业网站形象。
文章来源于/wiki/22652.html
责任编辑:
声明:本文由入驻搜狐号的作者撰写,除搜狐官方账号外,观点仅代表作者本人,不代表搜狐立场。
免费申请 SSL证书,全面支持微信小程序、阿里云、AWS、Apple ATS配置HTTPS
代码签名证书是保护软件版权的数字签名证书,通过软件代码对软件进行有效数字签名
今日搜狐热点下次自动登录
现在的位置:
& 综合 & 正文
nginx配置多端口多域名访问
linux 配置nginx多端口多域名
调了半年终于搞定,也不知道是什么问题,干!!
最后发现原因:(是.conf配件文件的格式不正确,win和linux有区别啊,安全起见还是拷贝原conf文件来改。)
1、先配置防火墙
有2种方法,一种是下面这样直接命令行敲,还有是修改etc下面的文件,最后记住保存一下,重启下防火墙。
开启防火墙:
/sbin/iptables -I INPUT -p tcp --dport 1010 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/sbin/iptables -I INPUT -p tcp --dport 1015 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
vi /etc/sysconfig/iptables
2、配置域名hots,其实就是域名解析,在本地的话直接修改hots文件如:#192.168.0.38
192.168.0.26
192.168.0.26
192.168.0.26
注意,这里绑定不要加端口号,其实还是绑定的原IP,这个测试规则太垃圾!!
直接通过IP和端口号也是可以直接访问的,只不过绑定了不同的域名。
这里我做了3个测试网址3、配置nginx.conf打开配置文件,路径:vi /usr/local/nginx/conf/nginx.conf其实这里没什么东东,看他这个块server{#一堆code,就是配置内容}include vhost/*.#加上这句话,意思是包含vhost/文件夹下所有.conf文件,相当于多个server{}块包含进来吧,分开写更清晰些新建vhost目录,并新建test80.conf,test1010.conf两个配置文件,这2个文件内容其实意思差不多,1个是精简版本,1个是完整版本,具体根据需要
#test80.conf文件内容:
listen 80;
location / {
/home/wwwroot/default/
index index.html index.
其实上面这种写法不好,在配置PHP时会出错,所以推荐的做法是把外面的大括号去除,即:
localtion/{}删除
listen 80;
/home/wwwroot/default/
index index.html index.
正确配置文件在本文最后
#test1010.conf内容:
index index.html index.htm index.
root /home/wwwroot/default/
#charset koi8-r;
#access_log
logs/host.access.
#error_page
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504
location = /50x.html {
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
proxy_pass
http://127.0.0.1;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass
127.0.0.1:9000;
fastcgi_index
#fastcgi_param
SCRIPT_FILENAME
/scripts$fastcgi_script_
include fastcgi.
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
index index.html index.htm index.
root /home/wwwroot/default2;
#charset koi8-r;
#access_log
logs/host.access.
#error_page
# redirect server error pages to the static page /50x.html
error_page
500 502 503 504
location = /50x.html {
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#location ~ \.php$ {
proxy_pass
http://127.0.0.1;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass
127.0.0.1:9000;
fastcgi_index
#fastcgi_param
SCRIPT_FILENAME
/scripts$fastcgi_script_
include fastcgi.
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#location ~ /\.ht {
正确配置文件:
index index.html index.htm index.
/home/wwwroot/default/b;
location ~ .*\.(php|php5)?$
try_files $uri =404;
fastcgi_pass
unix:/tmp/php-cgi.
fastcgi_index index.
include fcgi.
location /status {
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
location ~ .*\.(js|css)?$
access_log
/home/wwwlogs/access.
nginx.conf
worker_processes 1;
/home/wwwlogs/nginx_error.
/usr/local/nginx/logs/nginx.
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
worker_connections 51200;
default_type
application/octet-
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
gzip_min_length
gzip_buffers
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types
text/plain application/x-javascript text/css application/
gzip_proxied
expired no-cache no-
gzip_disable
"MSIE [1-6]\.";
#limit_zone
$binary_remote_addr
#log format
log_format
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
server_name www.lnmp.
index index.html index.htm index.
/home/wwwroot/
location ~ .*\.(php|php5)?$
try_files $uri =404;
fastcgi_pass
unix:/tmp/php-cgi.
fastcgi_index index.
include fcgi.
location /status {
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
location ~ .*\.(js|css)?$
access_log
/home/wwwlogs/access.
include vhost/*.
OK,都弄好了,最后重启下lngx即可。我这是一键包,直接/root/lnmp restart搞定。
访问成功!结束!
附一些相关文章
防火墙开启端口
http://www.blogjava.net/Alpha/archive//387640.html
/Article/48/66/.htm
nginx多站点配置
/index.php/archives/899
&&&&推荐文章:
【上篇】【下篇】nginx&多域名配置以及负载均衡[转]
nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里。
&&&&&一、每个域名一个文件的写法
首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers
,如要绑定域名&则在此目录建一个文件:.conf
然后在此文件中写规则,如:
listen&&&&&&
server_name&;&&&&&&&&&&&&&#绑定域名&
index index.htm index.html
index.&&&&&
#默认文件&
/home/;&&&&&&&&&&&&&&
#网站根目录
location.&&&&&&&&&&&&&&&&&&&&&&&&&&&
#调用其他规则,也可去除
然后重起nginx服务器,域名就绑定成功了
nginx服务器重起命令:/etc/init.d/nginx restart
&&&&二、一个文件多个域名的写法
一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:
listen&&&&&&
server_name&;&&&&&&&&&&&&&#绑定域名&
index index.htm index.html
index.&&&&&
#默认文件&
/home/;&&&&&&&&&&&&&&
#网站根目录
location.&&&&&&&&&&&&&&&&&&&&&&&&&&&
#调用其他规则,也可去除
listen&&&&&&
server_name&;&&&&&&&&&&&&&#绑定域名&
index index.htm index.html
index.&&&&&
#默认文件&
/home/www/;&&&&&&&&#网站根目录
location.&&&&&&&&&&&&&&&&&&&&&&&&&&&
#调用其他规则,也可去除
&&&&三、不带www的域名加301跳转
如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:
listen 80;
rewrite ^/(.*) /$1
&&&&四、添加404网页
添加404网页,都可又直接在里面添加,如:
listen&&&&&&
server_name&;&&&&&&&&&&&&&#绑定域名&
index index.htm index.html
index.&&&&&
#默认文件&
/home/;&&&&&&&&&&&&&&
#网站根目录
location.&&&&&&&&&&&&&&&&&&&&&&&&&&&
#调用其他规则,也可去除error_page 404&
最后还有一个方法需要注意,可能有需要禁止IP直接访问80端口或者禁止非本站的域名绑定我们的IP,这样的话应该
如下处理,放到最前一个server上面即可:
& &listen & 80
& &server_name &
& & return 403;
学会上面四种规则方法,基本就可以自己独立解决nginx 多域名配置问题了
模块官方介绍:&
http://wiki.nginx.org/HttpUpstreamModule&
http://www.howtocn.org/nginx:Nginx模块参考手册中文版
说说upstream里的server指令:&
后面可以是域名格式,也可以是socket格式[ip:port],后面还可以带参数。参数有下面几个:&
weight = NUMBER - 设置服务器的权重值,默认为1.
值越大,分配的请求越多。只适用于轮询这种LB策略。&
max_fails = NUMBER -
在fail_timeout设置的时间内,尝试连接服务器失败的次数.默认为1,0表示关闭检查.错误类型在proxy_next_upstream
fastcgi_next_upstream中定义,(除了404错误不计入max_fails).&
fail_timeout = TIME - the time during which must occur *max_fails*
number of unsuccessful attempts at communication with the server
that would cause the server to be considered inoperative, and also
the time for which the server will be considered inoperative
(before another attempt is made). If not set the time is 10
seconds. fail_timeout has nothing to do with upstream response
time, use proxy_connect_timeout and proxy_read_timeout for
controlling this.&
down - marks server as permanently offline, to be used with the
directive ip_hash.&
backup - (0.6.7 or later) only uses this server if the non-backup
servers are all down or busy (cannot be used with the directive
nginx负载均衡的策略:&
1.轮询(默认方式)&
对于一级后端服务器群,形成一个环队列的形式,对于每个到达的请求按时间顺序顺次分配给这些后端服务器。在前端调度器与后端服务器之间采用“心跳”方式进行状态检查,如果发现后端服务器宕机,则将其删除。&
这种方式为默认配置,优点是简洁,但缺点是无法进行最优化调度,有可能有的请求需要耗时较久,这样会带来一定的不平衡。&
它的例子:在http区域里添加:&
upstream lb {&
server 10.10.57.122:80;&
server 10.10.57.123:80;&
在你的某个server里增加:&
location / {&
&&&&&&&&&&&&&
proxy_pass http://&
2. 加权轮询&
这是一种对上述方式的改进,引入权值的概念,能够解决后端服务器性能不均的情况。&
例如这样一个配置:&
&&& upstream lb
server 10.10.57.122:80 weight=5;&
server 10.10.57.123:80
weight=10;&
ps:以上轮询负载均衡策略,我个人认为对于动态网站应用,这几乎就是形同摆设,没有人会采用。但一种情况例外:服务器端的session采用共享机制,如存储在数据库或者memcached内存里等。&
3. ip_hash(基于ip的hash分配策略)&
这是一种非轮询式方式,对于每个到达的请求,直接通过其请求IP进行哈希的映射,通过映射结果获得那一台后端服务器要处理这个请求,这种方式有一个明显的好处是能够保证session的唯一性。&
它的配置例子:&
upstream lb {&
server 10.10.57.122:80;&
server 10.10.57.123:80;&
在你的某个server里增加:&
location / {&
&&&&&&&&&&&&&
proxy_pass http://&
This directive causes requests to be distributed between upstreams
based on the IP-address of the
The key for the hash is the class-C network address of the client.
This method guarantees that the client request will always be
transferred to the same server. But if this server is considered
inoperative, then the request of this client will be transferred to
another server. This gives a high probability clients will always
connect to the same server.&
It is not possible to combine ip_hash and weight methods for
connection distribution. If one of the servers must be removed for
some time, you must mark that server as
由这段英文解说知道,客户端只要来自同一网段的ip的request都会转发到相同的后端服务器上。这里的所谓的class-C
network这里作者并没有很详细地解释,我只能说,写这句话的人不懂网络。我个人的理解是:以ip地址的点分十进制格式的前3个字节进行hash。&
其实这不是真正意义上的ip address hash,而只是network address hash。真正的ip address
hash方式有不?其实可以通过下面介绍的url_hash来实现。关键指令:hash
不过这里有个前提,$remote_addr必须是client的real ip
address。&
为什么这里能够实现真正意义上的ip address hash?很简单,就是这里整个ip
address被当作一个字符串来对待,故只要ip地址(key)不同,hash必然也是不同的。&
4. url_hash(基于URL的哈希方式)&
这种方式与IP的哈希方式类似,是对客户机请求的URL进行哈希操作,这样的方式有一个明显的好处是,能够便于内容缓存的实现,对于经常性的资源访问,采用这样的方式会获得非常好的质量。它目前不是nginx自带的功能,需要安装补丁方可使用。本指令的详细说明和安装见:(文章后面有附带详细安装实例)&
http://wiki.nginx.org/HttpUpstreamRequestHashModule&
它的配置方式为:&
upstream lb {&
server 10.10.57.122:80;&
server 10.10.57.123:80;&
hash $request_&
如果将这里的$request_uri换成$remote_addr便可实现上面我所说的真正基于ip地址的策略。&
5. 基于服务响应式&
&& 这种方式是根据服务器端的动态响应,对每一个请求进行分配。
这种方式能够自动根据当前的后端实际负载来优化。&
它的配置方式:&
upstream lb {&
server 10.10.57.122:80;&
server 10.10.57.123:80;&
这个没怎么测试,只是配起来用了下,机器差不多的话感觉就是轮询。&
===============================&
cd nginx-0.7.67;&
patch -p0 &
../nginx_upstream_hash-0.3.1/nginx.patch&
configure时:&
./configure --prefix=/app/nginx -user=nobody -group=nobody
--add-module=../nginx_upstream_hash-0.3.1/
--add-module=../gnosek-nginx-upstream-fair-2131c73/}

我要回帖

更多关于 nginx 域名指向目录 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信