How to configure nginx virtual host based on port

WBOY
Release: 2023-05-21 19:55:04
forward
1093 people have browsed it

1. Create the website directory and files:

[root@localhost data]# tree /data
/data
└── wwwroot
    ├── www.1.com_8080
    │   └── index.html
    └── www.1.com_8081
        └── index.html
Copy after login

2. Modify nginx.conf:

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    include vhost/*.conf;   #vhost目录下会包含所有的虚拟主机的配置文件
}
Copy after login

3. Create Virtual host configuration file directory:

[root@localhost conf]mkdir /usr/local/nginx/conf/vhost
Copy after login

4. Create a virtual host configuration file:

[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8080.conf 
server{
    listen 8080;
    server_name 1.com www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com_8080;
}
Copy after login
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8081.conf  
server{
    listen 8081;
    server_name 1.com www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com_8081;
}
Copy after login
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf          
server{
    listen 80 default_server;  #使用default_server指定nginx的默认虚拟主机
    deny all;
}
Copy after login

If you use other domain names to access the virtual host, it will match To the default virtual host, this configuration will reject virtual hosts with undefined domain names. If this option is not configured, the server ranked first will become the default virtual host.

5. Test whether there are problems with the configuration file:

[root@localhost root]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file
Copy after login

6. When the configuration file is modified, you can use the following command to reload the configuration file

[root@localhost sbin]# ./nginx -s reload
Copy after login

The above is the detailed content of How to configure nginx virtual host based on port. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template