Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Practice of Nginx Virtual Host configuration (1)

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Practice of Nginx Virtual Host configuration (1)

I. the concept of virtual host

In the Web service, the virtual host is an independent website site, which corresponds to an independent domain name (may also be IP or port), has an independent program and resource directory, and can independently provide services for users to access.

II. Types of virtual hosts

Virtual host based on domain name

Port-based virtual host

Virtual Host based on IP

Description: the most commonly used in actual production is the virtual host based on the domain name, the other two can be understood.

Third, the configuration of virtual host based on a domain name

Nginx main profile structure

Create a simplified Nginx master configuration file

[root@nginx-oldboy conf] # egrep-v "# | ^ $" nginx.conf.default > nginx.conf

Description: Nginx's main configuration file is nginx.conf,nginx.conf.default and nginx.conf content is the same.

After executing the above command, you get the following:

Modify the following:

12 server_name www.afeilinux.com

14 root html/wtf

Create the site directory and files corresponding to the domain name

[root@nginx-oldboy nginx1.10] # mkdir-p html/wtf

[root@nginx-oldboy nginx1.10] # cd html/ & & ls

50x.html index.html wtf

[root@nginx-oldboy html] # echo "first Test" >. / wtf/index.html

[root@nginx-oldboy html] # cat. / wtf/index.html

First test

Note: the function of the above command is to create a html/wtf site directory, which corresponds to the html/wtf setting (root html/wtf;) of the root root directory in the host configuration file. Then generate a default home page file, index.html, with the content of "first test".

Nginx syntax check and reload

[root@nginx-oldboy html] # nginx- t

[root@nginx-oldboy html] # nginx- s reload

Description: if you do not start nginx, you cannot reload. The error report is as follows:

View listening port

[root@nginx-oldboy html] # netstat-lnp | grep nginx

Modify hosts configuration file

[root@nginx-oldboy html] # echo "192.168.100.116 www.afeilinux.com" > > / etc/hosts

Check it out:

[root@nginx-oldboy html] # tail-N1 / etc/hosts

192.168.100.116 www.afeilinux.com

You also need to modify the hosts configuration file on the windows side.

Test the domain name site

IV. Configuration of virtual hosts based on multiple domain names

Add the configuration for the new domain name

There is already a configuration of a www.afeilinux.com virtual host above, and a configuration of a www.afeilinux.org virtual host is added below. The additional hosts must be within the http {} block of nginx.conf, preferably under the www.afeilinux.com virtual host configuration.

Server {

Listen 80

Server_name www.afeilinux.org

Location / {

Root html/org

Index index.html index.htm

}

Error_page 500 502 503 504 / 50x.html

Location = / 50x.html {

Root html

}

}

Create directories and files corresponding to the new virtual machine host site

[root@nginx-oldboy nginx1.10] # mkdir. / html/org

[root@nginx-oldboy nginx1.10] # echo "second Test" >. / html/org/index.html

[root@nginx-oldboy nginx1.10] # cat! $

Cat. / html/org/index.html

The second test

Check the site directory structure

[root@nginx-oldboy nginx1.10] # tree

-bash: tree: command not found

Solution:

[root@nginx-oldboy nginx1.10] # yum install-y tree

[root@nginx-oldboy nginx1.10] # tree html/

Check the syntax and reload the nginx configuration

[root@nginx-oldboy ~] # nginx- t

[root@nginx-oldboy ~] # nginx- s reload

Test the domain name site

Standardize and optimize nginx configuration files

Configure the virtual host as a separate configuration file from the nginx main configuration file nginx.conf

Description:

(1) it is suitable for situations where the number of virtual hosts is small.

(2) the sub-configuration files of all virtual hosts contained in the main configuration file will be placed in the extra directory.

(3) the virtual host is configured with a separate configuration file, which can be placed anywhere in the nginx main configuration file using the parameter include.

[root@nginx-oldboy conf] # mkdir extra

[root@nginx-oldboy conf] # sed-n'10 Magi 21p 'nginx.conf

[root@nginx-oldboy conf] # sed-n'10 nginx.conf 21p 'nginx.conf > extra/wtf.conf

[root@nginx-oldboy conf] # cat extra/wtf.conf

[root@nginx-oldboy conf] # sed-n'22 nginx.conf 33p 'nginx.conf > extra/org.conf

[root@nginx-oldboy conf] # cat extra/org.conf

Change the main profile nginx.conf

Delete the configuration of all virtual hosts in the main configuration file nginx.conf, including the server {} tag.

[root@nginx-oldboy conf] # sed-I'10 nginx.conf 33d'

[root@nginx-oldboy conf] # cat nginx.conf

Include the information of the virtual host independent configuration files wtf.conf and org.conf in the nginx.conf, thus separating the main configuration file from each virtual host configuration.

Include extra/wtf.conf

Include extra/org.conf

[root@nginx-oldboy conf] # sed-I'10 I include extra/wtf.conf;\ ninclude extra/org.conf;' nginx.conf

Nginx syntax detection and reload

[root@nginx-oldboy conf] # nginx- t

[root@nginx-oldboy conf] # nginx- s reload

test

[root@nginx-oldboy conf] # curl www.afeilinux.com

First test

[root@nginx-oldboy conf] # curl www.afeilinux.org

The second test

[root@nginx-oldboy conf] # tree extra/

This separates the virtual host configuration file from the nginx main configuration file!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report