Three ways to configure Nginx virtual hosts (based on domain names)

Three ways to configure Nginx virtual hosts (based on domain names)

Nginx supports three ways to configure virtual hosts: IP-based virtual host configuration, port-based virtual host configuration, and domain name-based virtual host configuration.

Detailed explanation of three ways to configure Nginx virtual hosts (based on IP) https://www.jb51.net/article/149774.htm

Detailed explanation of three ways to configure Nginx virtual hosts (based on ports) https://www.jb51.net/article/14978.htm

3. Nginx domain name-based virtual host configuration

Using domain name-based virtual host configuration is a popular method. Multiple domain names can be configured on the same IP and all accessed through port 80.

3.1 Assume that the server has an IP address of 192.168.2.155

[root@localhost ~]# ifconfig ens33:5 192.168.2.155/24 up
[root@localhost ~]# ifconfig
ens33:5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  inet 192.168.2.155 netmask 255.255.255.0 broadcast 192.168.2.255
  ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

3.2 The domain name corresponding to 192.168.2.155 is as follows. Configure the host file of the host for easy testing

[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts|grep 192.168.2.155
192.168.2.155 www.oa.com
192.168.2.155 www.bbs.com
192.168.2.155 www.test.com

3.3 Create a root directory for the virtual host to store web pages and create the homepage file index.html

[root@localhost ~]# cd /data/www/
[root@localhost www]# mkdir www.oa.com
[root@localhost www]# mkdir www.bbs.com
[root@localhost www]# mkdir www.test.com
[root@localhost www]# echo www.oa.com > www.oa.com/index.html
[root@localhost www]# echo www.bbs.com > www.bbs.com/index.html
[root@localhost www]# echo www.test.com > www.test.com/index.html

3.4 Modify nginx.conf and include the virtual host configuration file into the main file

[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost conf]# vim nginx.conf

Add the following configuration to the end of the nginx.conf file

# Find the following content in the http section and delete the "#" in front of each line
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';

# Add the following statement before the last "}" at the end of the configuration file, as shown below include vhost/*.conf

3.5 Edit the configuration file for each domain name (configuration file for each virtual host)

[root@localhost conf]# cd vhost/
[root@localhost vhost]# cat www.oa.com.conf
 server {
  listen 192.168.2.155:80;
  server_name www.oa.com;

  access_log /data/logs/www.oa.com.log main;
  error_log /data/logs/www.oa.com.error.log;

  location / {
   root /data/www/www.oa.com;
   index index.html index.htm;
  }
 }

[root@localhost vhost]# cat www.bbs.com.conf
 server {
  listen 192.168.2.155:80;
  server_name www.bbs.com;

  access_log /data/logs/www.bbs.com.log main;
  error_log /data/logs/www.bbs.com.error.log;

  location / {
   root /data/www/www.bbs.com;
   index index.html index.htm;
  }
 }

[root@localhost vhost]# cat www.test.com.conf
 server {
  listen 192.168.2.155:80;
  server_name www.test.com;

  access_log /data/logs/www.test.com.log main;
  error_log /data/logs/www.test.com.error.log;

  location / {
   root /data/www/www.test.com;
   index index.html index.htm;
  }
 }

[root@localhost vhost]# cat /data/www/www.oa.com/index.html
www.oa.com
[root@localhost vhost]# cat /data/www/www.bbs.com/index.html
www.bbs.com
[root@localhost vhost]# cat /data/www/www.test.com/index.html
www.test.com

3.6 Create a log file, otherwise nginx cannot be started

[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.oa.com.log
[root@localhost /]# touch /data/logs/www.oa.com.error.log
[root@localhost /]# touch /data/logs/www.bbs.com.log
[root@localhost /]# touch /data/logs/www.bbs.com.error.log
[root@localhost /]# touch /data/logs/www.test.com.log
[root@localhost /]# touch /data/logs/www.test.com.error.log
[root@localhost /]# ls /data/logs/
www.oa.com.error.log www.bbs.com.error.log www.test.com.error.log
www.oa.com.log www.bbs.com.log www.test.com.log

3.7 Test the configuration file before starting nginx

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# Start nginx
[root@localhost sbin]# ./nginx

3.8 Test Files

[root@localhost vhost]# curl http://www.oa.com
www.oa.com
[root@localhost vhost]# curl http://www.bbs.com
www.bbs.com
[root@localhost vhost]# curl http://www.test.com
www.test.com

Appendix: Problems during configuration

1. Problems that occurred during the final test

[root@localhost ~]# curl http://www.oa.com
curl: (7) Failed connect to www.oa.com:80; Connection refused

Solution:

Check whether Nginx is listening on the corresponding port.

[root@localhost ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 192.168.2.155:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp6 0 0 :::111 :::* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::23 :::* LISTEN
tcp6 0 0 ::1:25 :::* LISTEN

1. When configuring the virtual host file, add the listening IP address. Each virtual host configuration file is the same.

listen 192.168.2.155:80;

2. Restart the server after configuration is complete

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 set up virtual hosts and specified access paths in Nginx
  • Nginx virtual host setting example (multiple website configuration)
  • Detailed explanation of nginx virtual host configuration example
  • Detailed steps for configuring virtual hosts in nginx
  • In-depth analysis of Nginx virtual host
  • Summary of some unpopular knowledge about virtual hosts in Nginx

<<:  Vue implements drag progress bar

>>:  A simple example of mysql searching for data within N kilometers

Recommend

HTML marquee tag usage examples

This tag is not part of HTML3.2 and only supports ...

An example of refactoring a jigsaw puzzle game using vue3

Preface It took two days to reconstruct a puzzle ...

How to adjust the log level of nginx in Docker

Table of contents Intro Nginx Dockerfile New conf...

Basic introductory tutorial on MySQL partition tables

Preface In a recent project, we need to save a la...

Summary of some of my frequently used Linux commands

I worked in operations and maintenance for two ye...

Explain MySQL's binlog log and how to use binlog log to recover data

As we all know, binlog logs are very important fo...

MySQL master-slave data is inconsistent, prompt: Slave_SQL_Running: No solution

This article uses an example to describe the solu...

It's the end of the year, is your MySQL password safe?

Preface: It’s the end of the year, isn’t it time ...

Solve the problem of MySql client exiting in seconds (my.ini not found)

Problem description (environment: windows7, MySql...

WeChat applet implements a simple calculator

WeChat applet's simple calculator is for your...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...

Detailed explanation of crontab scheduled execution command under Linux

In LINUX, periodic tasks are usually handled by t...

A Brief Analysis of CSS Selector Grouping

Selector Grouping Suppose you want both the h2 el...

How to make Python scripts run directly under Ubuntu

Let’s take the translation program as an example....

MySQL compression usage scenarios and solutions

Introduction Describes the use cases and solution...