Example of how to configure multiple virtual hosts in nginx

Example of how to configure multiple virtual hosts in nginx

It is very convenient to configure virtual host vhost under nginx. Just add a server in the nginx configuration file nginx.conf

For example, I want to configure two virtual hosts, accessed through the domain names linux.com and linux2.com, and the corresponding directories are /usr/htdocs/linux and /usr/htdocs/linux2 respectively (this directory is where you put your development project files)

Because I installed a local virtual machine, I first added two redirects to the hosts file:

192.168.20.250 linux.com //192.168.20.250 is the IP address of my virtual machine, using bridge mode

192.168.20.250 linux2.com

Start configuring: Taking the configuration of linux.com as an example, configuring multiple virtual hosts is exactly the same process.

1. Find the nginx configuration file nginx.conf

Generally in the conf file under the nginx installation path

> Enter the conf directory and see many configuration files

Edit nginx.conf

Add a server{}, each server configuration corresponds to a virtual host vhost

server {
  listen 80; //Port 80 server_name linux.com; //Set the domain name #Directly enter the domain name to enter the directory and the default parsed file location / { 
    index index.html; 
    root /usr/htdocs/linux; //Enter linux.com directly to enter here, generally configure and parse the directory where php is located}

   #Parse .php file location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/htdocs/linux/$fastcgi_script_name; //The directory corresponding to the current virtual host include fastcgi_params;
  } 
}

Add in nginx.conf

Add the file index.php in the corresponding /usr/htdocs/linux for testing

Restart nginx: service nginx restart

After modifying the nginx configuration file, you need to restart nginx to take effect

Open the browser and enter the domain name to test:

OK, normal analysis!

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to configure nginx virtual hosts to access multiple websites on one server
  • Detailed explanation of how to configure virtual host vhost in nginx
  • Detailed explanation of the specific writing method of server_name in Nginx virtual host configuration
  • How to set up virtual hosts and specified access paths in Nginx
  • Nginx virtual host setting example (multiple website configuration)
  • Detailed steps for configuring virtual hosts in nginx

<<:  How to query data within a certain period of time with Vue front-end and Django back-end

>>:  A brief discussion on the calculation method of key_len in mysql explain

Recommend

How to automatically import Vue components on demand

Table of contents Global Registration Partial Reg...

How to install Jenkins on CentOS 8

To install Jenkins on CentOS 8, you need to use t...

Nginx configuration to achieve multiple server load balancing

Nginx load balancing server: IP: 192.168.0.4 (Ngi...

Detailed explanation of how to install PHP7 on Linux

How to install PHP7 on Linux? 1. Install dependen...

WeChat applet implements simple calculator function

WeChat applet: Simple calculator, for your refere...

The difference between html form submission action and url jump to actiond

The action of the form is different from the URL j...

Tips and precautions for using MySQL index

1. The role of index In general application syste...

MySQL 8.0.12 decompression version installation tutorial personal test!

Mysql8.0.12 decompression version installation me...

Detailed explanation of mysql permissions and indexes

mysql permissions and indexes The highest user of...

react-beautiful-dnd implements component drag and drop function

Table of contents 1. Installation 2.APi 3. react-...

Key points for writing content of HTML web page META tags

The META tag is an auxiliary tag in the head area...

Detailed steps for completely uninstalling MySQL 5.7

This article mainly summarizes various problems o...

Notes on Using Textarea

Why mention textarea specifically? Because the tex...

A brief discussion on size units in CSS

The compatibility of browsers is getting better a...