Ali cloud Linux system Nginx configuration of multiple domain name method

  • 2020-05-12 07:04:54
  • OfStack

Nginx can bind multiple domain names, which can be realized by writing multiple domain name rules into one configuration file, or by creating multiple domain name configuration files. For the convenience of management, it is recommended to build one file for each domain name, while some similar domain names can be written into one overall configuration file.

1. For example, I want to set up two websites with the domain names www.A.com and www.B.com, respectively, and resolve both of them to the same server IP.

2. In the Nginx configuration file conf directory, create a special directory to store the virtual host configuration, named vhosts, you can put all the configuration of the virtual directory here. For this example, create files named vhosts_A.conf and vhosts_B.conf and open them. Write the relevant configuration files in them.

Configuration file reference:


server {
  listen    80 default;# Listen on port 
  server_name www.A.com;# Binding domain 
  index index.html index.htm index.jsp login.jsp;# The default file 
  root /alidata/www/default;# Website root directory 
  location / 
  {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect off;
    proxy_set_header  Host  $host; 
    proxy_set_header  X-Real-IP  $remote_addr; 
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for; 
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  } 

  location ~ .*\.(js|css)?$
  {
    expires 1h;
  } 

  access_log /alidata/log/nginx/access/default.log;
} 

3. In the nginx master profile, set the profiles that contain the two sites.

Open the nginx.conf file in http{... } section of the input


  # Contains configuration files for all virtual hosts   (fill in the server vhosts_conf/*.conf Absolute path) 
  include /alidata/server/nginx/conf/vhosts/*.conf; 

4. Upload files for each site in the specified root directory

5. Restart nginx: / etc init d/nginx restart, test whether access to normal


Related articles: