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 set up remote access to a server by specifying an IP address in Windows

We have many servers that are often interfered wi...

Use tomcat to set shared lib to share the same jar

As more and more projects are deployed, more and ...

jQuery implements dynamic tag event

This article shares the specific code of jQuery t...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

Linux hardware configuration command example

Hardware View Commands system # uname -a # View k...

Front-end vue+express file upload and download example

Create a new server.js yarn init -y yarn add expr...

MySQL Server 8.0.13.0 Installation Tutorial with Pictures and Text

Install 8.0.13 based on MySQL 6.1.3. MySQL 8.0.13...

How to uninstall and reinstall Tomcat (with pictures and text)

Uninstall tomcat9 1. Since the installation of To...

In-depth understanding of umask in new linux file permission settings

Preface The origin is a question 1: If your umask...