Nginx:一个 IP,多个网站,如何配置?

2015-09-16 23:32:27 +08:00
 linar

一个 IP ,多个网站,如何配置?

ubuntu 14.04 nginx 1.80 php5-fpm

一个网站的时候正常,在 sites-available 再添加一个配置文件的时候,全部无法访问,请教如何正确配置

10933 次点击
所在节点    NGINX
47 条回复
cxbig
2015-09-16 23:34:50 +08:00
监听同样的端口 :80 和 :443 ,然后 ServerName 不一样的就行,指定不一样的 Directory 路径。
Daddy
2015-09-16 23:35:05 +08:00
是放 conf 的鏈接,不是 conf
Scholer
2015-09-16 23:35:14 +08:00
server_name 想加多少加多少
cxbig
2015-09-16 23:37:37 +08:00
sites-available 是让你准备更多的独立的配置文件
你要让它们生效,需要连接到 sites-enabled ,然后重启 nginx 服务
linar
2015-09-16 23:38:59 +08:00
@Daddy 麻烦请详细说明,谢谢。

我想做的是在一个单 IP 服务器上,有 3 个域名,想建 3 个 wordpress 站点。
Starduster
2015-09-16 23:39:31 +08:00
在 nginx.conf 里添加不同 Server 区块,或者在 conf.d/下添加独立的文件
server {
listen 80;
server_name aaa;
root /xxx/aaa/;
}
server {
listen 80;
server_name bbb;
root /xxx/bbb/;
}
http://nginx.org/en/docs/http/server_names.html
Daddy
2015-09-16 23:45:46 +08:00
nginx 比較搞,不同版本都有变化,/usr/share/nginx 目录有,/etc/nginx 目录也有,都不统一的。我就是不喜欢,统一用一键包,就算只装一个 nginx 也是。
我记得是需要用 ln 做一下链接的。默认给的也是一个 ln 链接
loveminds
2015-09-16 23:46:31 +08:00
@Daddy 还有 /usr/local/nginx
fengdra
2015-09-16 23:51:42 +08:00
server {
listen 80;
return 403; # 禁止 IP 访问
}

server { #第一个网站
listen 80;

root ....;
index ....;

server_name xxx.com; #域名

location / {
...
}
...
}

server { #第二个网站
listen 80;

root ....;
index ....;

server_name yyy.com;

location / {
...
}
...
}
cxbig
2015-09-16 23:53:54 +08:00
LZ 说了有 sites-available ,那么肯定还有个 sites-enabled , link 上就好,分散安排不容易冲突,都写一个文件里太乱
Daddy
2015-09-16 23:57:26 +08:00
@loveminds 找死人的,我说怎么找不着目录,难道装出问题了?还有呢,一会是 conf.d ,一会是 sites-available , conf.d 是直接放 conf ,而 sites-available 是弄成软链接,我 X
linar
2015-09-16 23:58:10 +08:00
@cxbig 我就是建立了不同的配置文件在 sites-available,然后 ln 到 sites-enabled ,然后,然后就都无法访问了。
刚开始只添加一网站,可以正常访问,当我再添加一个配置文件到 sites-available ,并且 ln 后,两个网站就都无法访问了。。。
cxbig
2015-09-17 00:00:52 +08:00
@linar 去 sites-enabled 用 ls -alh 贴结果出来,我看看你的 ln 是不是用对了
billlee
2015-09-17 00:03:29 +08:00
首先看看日志里报了什么错吧
cxbig
2015-09-17 00:04:37 +08:00
@linar 以下两个语法都可以:
1. sites-enabled$ sudo ln -s ../sites-available/site1 .
2. ~$ sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
如果你确定链接没有问题,请检查每个配置文件的语法是不是有错
linar
2015-09-17 00:05:45 +08:00
@cxbig ln -s /etc/ngixn/sites-available/xxx /etc/nginx/sites-enabled/xxx
而且一旦我把后来添加的配置文件删除,重启 nginx 后,第一个网站就又恢复正常了
cxbig
2015-09-17 00:07:00 +08:00
@linar 那就查那个 xxx 配置文件,配合查看 nginx 的 log
XiaoxiaoPu
2015-09-17 00:07:56 +08:00
@Daddy 其实 Nginx 不搞, Nginx 自身是没有 sites-available 这种东西的,连 conf.d 都没有,只有一个 /etc/nginx/nginx.conf ,这些文件夹都是各个 debian 系, rhel 系发行版打包时自己加上的。
linar
2015-09-17 00:11:54 +08:00
@cxbig
2015/09/16 23:19:37 [warn] 1902#0: conflicting server name "www.xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:19:37 [warn] 1902#0: conflicting server name "xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:19:37 [emerg] 1902#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2015/09/16 23:19:37 [warn] 1905#0: conflicting server name "www.xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:19:37 [warn] 1905#0: conflicting server name "xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:19:37 [emerg] 1905#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2015/09/16 23:22:33 [warn] 2098#0: conflicting server name "www.xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:22:33 [warn] 2098#0: conflicting server name "xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:22:33 [emerg] 2098#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2015/09/16 23:41:57 [emerg] 3139#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2015/09/16 23:43:09 [warn] 3287#0: conflicting server name "www.xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:43:09 [warn] 3287#0: conflicting server name "xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:43:09 [warn] 3287#0: conflicting server name "www.xxx2.com" on 0.0.0.0:80, ignored
2015/09/16 23:43:09 [warn] 3287#0: conflicting server name "xxx2.com" on 0.0.0.0:80, ignored
2015/09/16 23:43:09 [emerg] 3287#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
2015/09/16 23:45:50 [warn] 3529#0: conflicting server name "www.xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:45:50 [warn] 3529#0: conflicting server name "xxx.com" on 0.0.0.0:80, ignored
2015/09/16 23:45:50 [warn] 3529#0: conflicting server name "www.xxx2.com" on 0.0.0.0:80, ignored
2015/09/16 23:45:50 [warn] 3529#0: conflicting server name "xxx2.com" on 0.0.0.0:80, ignored
2015/09/16 23:45:50 [emerg] 3529#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
Daddy
2015-09-17 00:13:18 +08:00
@XiaoxiaoPu 没研究,只是看着就烦,变来变去的,目录一会是 /etc/nginx ,一会儿又去了 /usr/share/nginx ,还有 /usr/local/nginx ,然后干脆就统一用自己熟悉的一键包安装 nginx ,方便自己。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/221270

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX