How to configure virtual hosts in Nginx, and how to configure virtual hosts in nginx
It is to start multiple websites on one server.
There are two ways to differentiate different websites:
1. Different domain names
2. Different ports
Differentiate virtual machines through ports
Nginx configuration file:
/usr/local/nginx/conf/nginx.conf
# User nobody; worker_processes 1; # error_log logs/error. log; # error_log logs/error. log notice; # error_log logs/error. log info; # pid logs/nginx. pid; events {worker_connections 1024;} http {include mime. types; default_type application/octet-stream; # log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"' # '$ status $ response "$ http_referer"' # '"$ http_user_agent" "$ http_x_forwarded_for "'; # access_log logs/access. log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # gzip on; # A server node is a virtual host server {# port listen 80; server_name localhost; # charset koi8-r; # access_log logs/host. access. log main; location/{# Html is the root html directory under the nginx installation directory; index index.html index.htm ;}}}
You can configure multiple servers and multiple virtual hosts.
Add a VM:
# User nobody; worker_processes 1; # error_log logs/error. log; # error_log logs/error. log notice; # error_log logs/error. log info; # pid logs/nginx. pid; events {worker_connections 1024;} http {include mime. types; default_type application/octet-stream; # log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request"' # '$ status $ response "$ http_referer"' # '"$ http_user_agent" "$ http_x_forwarded_for "'; # access_log logs/access. log main; sendfile on; # tcp_nopush on; # keepalive_timeout 0; keepalive_timeout 65; # gzip on; server {listen 80; server_name localhost; # charset koi8-r; # access_log logs/host. access. log main; location/{root html; index index.html index.htm ;}# copy a server, modify the relevant information on the server {listen 81; server_name localhost; # charset koi8-r; # access_log logs/host. access. log main; location/{root html81; index index.html index.htm ;}}}
Reload the configuration file
[Root @ localhost nginx] # sbin/nginx-s reload
Copy html directory
[Root @ nginx] # cp-r html html81
Modify related content
[Root @ nginx] # vi html81/index.html
Reload the configuration file
[Root @ localhost nginx] # sbin/nginx-s reload
Access through a browser:
Successful!