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
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目录下会包含所有的虚拟主机的配置文件 }
3. Create Virtual host configuration file directory:
[root@localhost conf]mkdir /usr/local/nginx/conf/vhost
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; }
[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; }
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf server{ listen 80 default_server; #使用default_server指定nginx的默认虚拟主机 deny all; }
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
6. When the configuration file is modified, you can use the following command to reload the configuration file
[root@localhost sbin]# ./nginx -s reload
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!