在 nginx 和网站上的多个网站-可用

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

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

71177 次浏览

如果查看 nginx.conf,您将发现 include指令包含来自 sites-enabled目录的所有文件。此目录存储来自 sites-available的配置文件的符号链接,以便于打开和关闭配置的各个部分。

如你所见,这些目录没有魔力。

如果你想托管多个网站,你应该使用多个 server块和/或 server_name指令。官方教程在这里: 服务器名称Nginx 如何处理请求

只要添加另一种方法,您就可以为托管的每个虚拟域或站点使用单独的文件。 可以使用默认副本作为每个站点的起点,并为每个站点进行自定义。
Then create symlinks in sites-enabled. In this way you can take sites up and down just by adding or removing a symlink and issuing a service nginx reload.

当您进行网站维护时,您可以创造性地使用此方法将网站重定向到维护模式页面。

结构是这样的:

/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

Notice that since there are only the first two entries are the only symlinked items in sites-enabled, the third entry, someOtherSite.com is therefore offline.

You symlink the default file from sites available to sites enabled. Then you modify the available site to include two server blocks each with a different server_name. see the following. This assumes you have to domains called example.com and example2.com. You would also have pointed your @records to the ip address of the server where you've installed nginx.

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

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

edit the file using your editor of choice (vim for me)

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/;
}


}