English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Nginx Configuration for Virtual Hosts, Method to Access Multiple Websites on One Server

Access different websites on a single server

There are usually two ways to distinguish:

1Access through the listening port:

2Access through the domain name:

1Access different hosts through the port:

The configuration file of Nginx:

/usr/local/nginx/conf/nginx.conf

The default encoding format of Centos files is latin1

The command to check the encoding format: :set fileencoding

#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;
}
##A http node
http {  
  include    mime.types;
  default_type application/octet-stream;
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  #         '$status $body_bytes_sent "$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;
 #The server node is the configuration of the website you need to access
 #A server node is a virtual host
  server {
    listen    80;  #The port number being monitored, the default port for accessing the website is80 port
    server_name localhost;  #That is the domain name to be accessed
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {  #Locate
      root  html;  #Locate the html folder under the nginx root directory
      index index.html index.htm;  #Set the website homepage
    }
  }
}

At this time, you can configure multiple servers, which means you have configured different hosts

Add a virtual host: (distinguish by port number)

server {
    listen    81;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root html-81;
  #New html under the root directory of nginx81 Folder
      index index.html index.htm;
    
  }

After editing the file, we reload the configuration file

Through the command: ./nginx -s reload

Effect:

We know that when multiple websites are configured on a server, it is impossible to distinguish them by port number, so I need to distinguish them by domain name next

2It uses domain names to distinguish different virtual hosts

What is a domain name??

Domain name is the website address

For example: www.baidu.com

Usually, when we access a domain name, we need to resolve the domain name through a DNS server

Dns server: It resolves domain names to IP addresses. It saves the mapping relationship between domain names and IP addresses.

One domain name corresponds to one IP address, and one IP address can be bound to multiple domain names.

Local testing can modify the hosts file.

Modify the Windows hosts file: (C:\Windows\System32\drivers\etc)

You can configure the mapping relationship between domain names and IPs. If the hosts file is configured with the corresponding relationship between domain names and IPs, there is no need to go through the DNS server!!!!!!!

Continue to configure in the nginx.conf file just mentioned:

server {
    listen    80;
    server_name www.taobao.com;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root html-taobao;
      index index.html index.htm;
    }
  }
  server {
    listen    80;
    server_name www.baidu.com;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root html-baidu;
      index index.html index.htm;
    }
  }
}

Domain name configuration:

192.168.25.148 www.test.com
192.168.25.148 www.yiyou.com

Restart the nginx service

Observe the effect:

The following nginx configuration for virtual hosts, which is the method of accessing multiple websites on a single server that the editor shares with everyone, is all the content shared by the editor. I hope it can give everyone a reference, and I also hope everyone will support the Yanao tutorial more.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please send an email to codebox.com (replace # with @ when sending an email) to report any violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like