75

使用 nginx 的基本安装,您的sites-available文件夹只有一个文件:default

sites-available文件夹是如何工作的,我将如何使用它来托管多个(单独的)网站?

4

3 回答 3

90

只是为了添加另一种方法,您可以为您托管的每个虚拟域或站点使用单独的文件。您可以使用默认副本作为每个副本的起点,并为每个站点进行自定义。
然后在启用站点中创建符号链接。通过这种方式,您可以通过添加或删除符号链接并发出服务 nginx 重新加载来启动和关闭站点。

在进行站点维护时,您可以发挥创意并使用此方法将站点重定向到维护模式页面。

所以结构看起来像这样:

/sites-available/ (you can use obvious file names like this)
| 
|-> a.mysite.com
|-> b.mysite.com
|-> someOtherSite.com

/sites-enabled/ (these are just symlinks to the real files in /sites-available)
| 
|-> a.mysite.com
|-> b.mysite.com

请注意,由于只有前两个条目是 中唯一的符号链接项sites-enabled,因此第三个条目someOtherSite.com是脱机的。

于 2012-07-28T14:44:50.617 回答
30

If you look at nginx.conf, you will find include directive that includes all files from the sites-enabled directory. This directory stores symlinks to config files from sites-available in order to be convenient to switch on and off parts of your configuration.

As you can see, there's no magic with these directories.

If you want to host multiple websites you should use multiple server blocks and/or server_name directive. Official tutorials are here: Server names and How nginx processes a request.

于 2012-07-27T20:22:40.303 回答
19

您将默认文件从可用站点符号链接到已启用站点。然后修改可用站点以包括两个服务器块,每个块具有不同的 server_name。见下文。这假设您必须拥有名为 example.com 和 example2.com 的域。您还可以将@records 指向您安装了nginx 的服务器的IP 地址。

将可用站点符号链接到启用的站点

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

使用您选择的编辑器编辑文件(对我来说是 vim)

sudo vi /etc/nginx/sites-available/default

这是工作 nginx conf 的内容,假设您在端口 4567 和 4568 上运行 Web 应用程序。

server {

    server_name www.example.com

    location / {
        proxy_pass http://localhost:4567/;
    }

}


server {

    server_name www.example2.com

    location {
        proxy_pass http://localhost:4568/;
    }

}
于 2013-10-19T21:22:28.720 回答