Linux nginx 上的多个网站和可用站点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11693135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 13:56:43  来源:igfitidea点击:

multiple websites on nginx & sites-available

linuxnginxwebserver

提问by Kristian

With the base install of nginx, your sites-availablefolder has just one file: default

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

how does the sites-availablefolder work and how would I use it to host multiple (separate) websites?

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

采纳答案by Carlos

Just to add another approach, you can use a separate file for each virtual domain or site you're hosting. You can use a copy of default as a starting point for each one and customize for each site.
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.

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

You can get creative and use this method to redirect sites to a maintenance mode page while you are doing site maintenance.

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

So the structure looks like this:

所以结构看起来像这样:

/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.comis therefore offline.

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

回答by VBart

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

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

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

如您所见,这些目录没有什么神奇之处。

If you want to host multiple websites you should use multiple serverblocks and/or server_namedirective. Official tutorials are here: Server namesand How nginx processes a request.

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

回答by jmontross

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.

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

symlink the available site to an enabled site

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

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

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

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

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

Here's contents of working nginx conf, assuming you are running web apps on the port 4567 and 4568.

以下是工作 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/;
    }

}