NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
linar
V2EX  ›  NGINX

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

  •  
  •   linar · Sep 16, 2015 · 11794 views
    This topic created in 3927 days ago, the information mentioned may be changed or developed.

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

    ubuntu 14.04 nginx 1.80 php5-fpm

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

    47 replies    2015-09-17 23:29:55 +08:00
    cxbig
        1
    cxbig  
       Sep 16, 2015
    监听同样的端口 :80 和 :443 ,然后 ServerName 不一样的就行,指定不一样的 Directory 路径。
    Daddy
        2
    Daddy  
       Sep 16, 2015
    是放 conf 的鏈接,不是 conf
    Scholer
        3
    Scholer  
       Sep 16, 2015
    server_name 想加多少加多少
    cxbig
        4
    cxbig  
       Sep 16, 2015
    sites-available 是让你准备更多的独立的配置文件
    你要让它们生效,需要连接到 sites-enabled ,然后重启 nginx 服务
    linar
        5
    linar  
    OP
       Sep 16, 2015
    @Daddy 麻烦请详细说明,谢谢。

    我想做的是在一个单 IP 服务器上,有 3 个域名,想建 3 个 wordpress 站点。
    Starduster
        6
    Starduster  
       Sep 16, 2015   ❤️ 1
    在 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
        7
    Daddy  
       Sep 16, 2015
    nginx 比較搞,不同版本都有变化,/usr/share/nginx 目录有,/etc/nginx 目录也有,都不统一的。我就是不喜欢,统一用一键包,就算只装一个 nginx 也是。
    我记得是需要用 ln 做一下链接的。默认给的也是一个 ln 链接
    loveminds
        8
    loveminds  
       Sep 16, 2015
    @Daddy 还有 /usr/local/nginx
    fengdra
        9
    fengdra  
       Sep 16, 2015   ❤️ 1
    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
        10
    cxbig  
       Sep 16, 2015
    LZ 说了有 sites-available ,那么肯定还有个 sites-enabled , link 上就好,分散安排不容易冲突,都写一个文件里太乱
    Daddy
        11
    Daddy  
       Sep 16, 2015
    @loveminds 找死人的,我说怎么找不着目录,难道装出问题了?还有呢,一会是 conf.d ,一会是 sites-available , conf.d 是直接放 conf ,而 sites-available 是弄成软链接,我 X
    linar
        12
    linar  
    OP
       Sep 16, 2015
    @cxbig 我就是建立了不同的配置文件在 sites-available,然后 ln 到 sites-enabled ,然后,然后就都无法访问了。
    刚开始只添加一网站,可以正常访问,当我再添加一个配置文件到 sites-available ,并且 ln 后,两个网站就都无法访问了。。。
    cxbig
        13
    cxbig  
       Sep 17, 2015
    @linar 去 sites-enabled 用 ls -alh 贴结果出来,我看看你的 ln 是不是用对了
    billlee
        14
    billlee  
       Sep 17, 2015
    首先看看日志里报了什么错吧
    cxbig
        15
    cxbig  
       Sep 17, 2015
    @linar 以下两个语法都可以:
    1. sites-enabled$ sudo ln -s ../sites-available/site1 .
    2. ~$ sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/
    如果你确定链接没有问题,请检查每个配置文件的语法是不是有错
    linar
        16
    linar  
    OP
       Sep 17, 2015
    @cxbig ln -s /etc/ngixn/sites-available/xxx /etc/nginx/sites-enabled/xxx
    而且一旦我把后来添加的配置文件删除,重启 nginx 后,第一个网站就又恢复正常了
    cxbig
        17
    cxbig  
       Sep 17, 2015
    @linar 那就查那个 xxx 配置文件,配合查看 nginx 的 log
    XiaoxiaoPu
        18
    XiaoxiaoPu  
       Sep 17, 2015
    @Daddy 其实 Nginx 不搞, Nginx 自身是没有 sites-available 这种东西的,连 conf.d 都没有,只有一个 /etc/nginx/nginx.conf ,这些文件夹都是各个 debian 系, rhel 系发行版打包时自己加上的。
    linar
        19
    linar  
    OP
       Sep 17, 2015
    @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
        20
    Daddy  
       Sep 17, 2015
    @XiaoxiaoPu 没研究,只是看着就烦,变来变去的,目录一会是 /etc/nginx ,一会儿又去了 /usr/share/nginx ,还有 /usr/local/nginx ,然后干脆就统一用自己熟悉的一键包安装 nginx ,方便自己。
    cxbig
        21
    cxbig  
       Sep 17, 2015
    @linar 把 emerg 那个问题处理掉,去总配置文件下加个这个试试
    http {
    server_names_hash_bucket_size 64;
    ...
    }
    jarlyyn
        22
    jarlyyn  
       Sep 17, 2015
    这个都不会的话,千万别用 nginx.

    应为你估计也没法把.htaccess 里的安全配置改成 nginx 的
    msg7086
        23
    msg7086  
       Sep 17, 2015
    @Daddy 你需要习惯系统包……
    Daddy
        24
    Daddy  
       Sep 17, 2015
    @msg7086 半桶水,不想记太多把自己都搞蒙了,会一个就可以了,其他慢慢学习消化吧。
    就比如这个问题,这个坑踩过,所以第一时间还是能猜到 LZ 是软链接那里弄错了,而不是乱贴 conf
    stanhou
        25
    stanhou  
       Sep 17, 2015
    这种最基本的问题应该自己去查手册
    iyaozhen
        26
    iyaozhen  
       Sep 17, 2015
    我觉得这个问题默认的 conf 文件里面就有写呀。去掉注释就能运行
    oojiayu
        27
    oojiayu  
       Sep 17, 2015 via Smartisan T1
    安装个 AMH 不就完了么,至于那么麻烦?
    jamesliu96
        28
    jamesliu96  
       Sep 17, 2015
    在 http {} 里面加 server {}
    icloudnet
        29
    icloudnet  
       Sep 17, 2015
    一直是 Apache 死忠,所以也是 centos 死忠,这段时间搞树莓派,用了 Apache2 了,还是不习惯,但是还是不用 nginx ,虽说 nginx 占用内存少点,主要还是再学习成本比 Apache 要高。
    molinxx
        30
    molinxx  
       Sep 17, 2015 via iPhone
    AMH 、 LuManager 、 WDCP ,不然来个 LNMP 一键包也行,生产环境最好等自己在测试环境折腾明白了再上。
    ToysMall
        32
    ToysMall  
       Sep 17, 2015 via iPhone
    这种水平就来?
    XianZaiZhuCe
        33
    XianZaiZhuCe  
       Sep 17, 2015
    这东西百度一下不太难的
    fengyqf
        35
    fengyqf  
       Sep 17, 2015
    @msg7086 +86400
    搭车推荐用发行版编译好的包( rpm , deb 等)这些包的目录一致性好,相关文件容易找。
    初学者可能会觉得乱,不像 win 程序,一个软件一个独立目录;一旦熟悉后,你绝对不想“回去”。
    当然,自己编译的话,推荐参考这个规范,只不过放到 /usr/local 里,即 /usr/local/bin, /usr/local/etc, /usr/local/lib, /usr/local/share 等。当然编译前的配置有点麻烦。

    @Daddy
    @loveminds
    flydogs
        36
    flydogs  
       Sep 17, 2015
    lnmp 一键安装,
    然后可以用命令添加网站 /域名。
    Admstor
        37
    Admstor  
       Sep 17, 2015
    lnmp.org
    还是比较稳定的
    用在生产环境问题也不大
    ljbha007
        38
    ljbha007  
       Sep 17, 2015
    @linar
    log 说的很清楚
    你两个域名重名了
    肯定一个是父域名 一个是父域名下的二级子域名
    lightforce
        39
    lightforce  
       Sep 17, 2015
    ln -s .../sites-available/xxx .../sites-enabled/
    loveminds
        40
    loveminds  
       Sep 17, 2015
    @fengyqf 我认为应该是,/etc/软件名字 /xxx.conf
    fengyqf
        41
    fengyqf  
       Sep 17, 2015
    @loveminds 你的意思是:
    手工编译的包,其配置文件应该写到 /etc/软件名字 /xxx.conf
    如果你的配置文件名字正好跟系统包管理的文件名字一致,包管理工具你认为你装过这个包,在做包 update 时,可能给你把这个包安装上。
    我以为遇到过类似情况,自己编译的新版本 php ,然后 yum update 后,我的配置被覆盖了
    aveline
        42
    aveline  
       Sep 17, 2015
    @Daddy 不统一是因为不是用系统包装的吧
    ys0290
        43
    ys0290  
       Sep 17, 2015 via iPhone
    多看 log
    hippoboy
        44
    hippoboy  
       Sep 17, 2015
    楼上好多人说了,server name
    loveminds
        45
    loveminds  
       Sep 17, 2015
    @fengyqf 那种情况,旧的配置文件应该会备份为.rpmsave
    fengyqf
        46
    fengyqf  
       Sep 17, 2015
    @loveminds 有自动改名备份没错,但手工编译的软件没了,莫名其妙的版本变了。
    为了不给自己找麻烦,所以推荐放到 /usr/local/ 下的子目录,这样不会跟包管理冲突。
    loveminds
        47
    loveminds  
       Sep 17, 2015
    @fengyqf 用 ln 指向也可以
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5092 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 152ms · UTC 08:01 · PVG 16:01 · LAX 01:01 · JFK 04:01
    ♥ Do have faith in what you're doing.