nginx configuration multi-domain multi-site Ubuntu
1. Install nginx
apt install nginx
2. Configuration file description
The nginx configuration file is in /etc/nginx
the directory, and its default content is like this
root@2bd0:/etc/nginx# ll
total 72
drwxr-xr-x 8 root root 4096 Jul 31 15:21 ./
drwxr-xr-x 104 root root 4096 Aug 1 09:54 ../
drwxr-xr-x 2 root root 4096 Nov 10 2022 conf.d/
-rw-r--r-- 1 root root 1077 Feb 4 2019 fastcgi.conf
-rw-r--r-- 1 root root 1007 Feb 4 2019 fastcgi_params
-rw-r--r-- 1 root root 2837 Feb 4 2019 koi-utf
-rw-r--r-- 1 root root 2223 Feb 4 2019 koi-win
-rw-r--r-- 1 root root 3957 Feb 4 2019 mime.types
drwxr-xr-x 2 root root 4096 Nov 10 2022 modules-available/ #
drwxr-xr-x 2 root root 4096 Jul 31 15:21 modules-enabled/
-rw-r--r-- 1 root root 1490 Feb 4 2019 nginx.conf
# 默认 配置文件,一般不需要修改这个文件,为了方便管理,它会包含下面 sites-enabled 内的所有内容
-rw-r--r-- 1 root root 180 Feb 4 2019 proxy_params
-rw-r--r-- 1 root root 636 Feb 4 2019 scgi_params
drwxr-xr-x 2 root root 4096 Aug 1 09:06 sites-available/
# 所有可用的站点配置文件,启用和未启用的一般都放这里面,看名字就知道什么意思
drwxr-xr-x 2 root root 4096 Aug 1 09:07 sites-enabled/
# 所有已启用的都在这个文件夹中,一般是一些链接文件
drwxr-xr-x 2 root root 4096 Jul 31 15:21 snippets/
-rw-r--r-- 1 root root 664 Feb 4 2019 uwsgi_params
-rw-r--r-- 1 root root 3071 Feb 4 2019 win-utf
3. Configure multi-domain and multi-site
Tell me how to configure multiple domain names and multiple sites on one server.
There are several ways to implement multisite:
- Multiple Domains, Multiple Sites
- multi-port, multi-site
For example, a.com
b.com
both point to the server 1.2.3.4
. What we need to realize is that different websites appear when accessing these two domain names. They both serve on port 80. That is to say, there is no need to access like a.com:8080
b.com:8081
this, but a.com
a website appears directly when accessing , b.com
another website appears when you visit it.
The default website file of nginx is in /var/www/html
the folder. Generally, there is no special requirement and no need to modify it. Just put the file in this directory and use it.
1. Create a site configuration file
Add a file in /etc/nginx/sites-available
the foldera.com
cd /etc/nginx/sites-available
vi a.com
Add the following:
server {
listen 80; # 监听 80 端口,但只有用 a.com 访问的时候才会匹配到这个配置
server_name a.com; # 这里是 a.com 的域名
root /var/www/html/a.com; # 这里是 a.com 的根目录
index index.html index.htm;
}
same b.com
reason
At this point, sites-available
there are already two files in
a.com
b.com
2. Enable Site Profiles
There is a knowledge point that needs to be explained. When you check it nginx.conf
, you can see such a sentence, as shown below.
It means to include site-enabled/
all the configurations below
Above we have sites-available
created two websites in the directory. Now we need to sites-enabled
create a link file of these two configuration files in the folder, the meaning of the link file is similar to the shortcut in the Windows desktop, it is a link. But when using it, it is the same as using the source file.
cd /etc/nginx/sites-enabled
ln -s ../sites-available/a.com a.com
ln -s ../sites-available/b.com b.com
This sites-enabled
creates two linked files in the folder. This is equivalent to having two files sites-enabled
in the directory that are exactly the same as the directorysites-available
like this
3. Restart nginx to make it take effect
systemctl restart nginx
Four, finished
In this way, when accessing, it is to access the contents of the directory a.com
on the server. When accessing, it is to access the contents of the directory on the server/var/www/html/a.com/
b.com
/var/www/html/b.com/