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

Build nginx virtual host based on domain name, port and IP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to build a nginx virtual host? This problem may be often seen in our daily work. Through this question, I hope you can gain more. Here are the steps to solve this problem.

There are three types of virtual hosts supported by Nginx

1. Virtual host based on domain name

2. Virtual host based on IP

3. Port-based virtual host

And each kind of virtual host can realize its own function through "server {}" configuration segment.

First, build based on domain name

1. Compile and install Nginx service

2. Obtain the source code package on Windows remotely and mount it to Linux

[root@localhost] # smbclient-L / / 192.168.235.1Enter SAMBA\ root's password: Sharename Type Comment- LNMP Disk [root@localhost ~] # mkdir / abc [root@localhost ~] # mount.cifs / / 192.168.235.1/LNMP / abcPassword for root@//192.168.235.1/LNMP: [root @ localhost ~] # ls / abcDiscuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.10.tar.bz2mysql-boost-5.7.20.tar.gz nginx-1.12.2.tar.gz php-7.1.20.tar.gz

3. Decompress the source package, download and install the compiler package

[root@localhost ~] # cd / abc [root@localhost abc] # tar zxvf nginx-1.12.0.tar.gz-C / opt [root@localhost abc] # ls / optnginx-1.12.0 rh [root@localhost abc] # cd / opt [root@localhost opt] # yum install-y\ > gcc\ / / C language > gcc-c++\ / / C++ language > pcre-devel\ / / pcre language tool > zlib -devel / / compressed function library

4. Create program users and configure Nginx service-related components

[root@localhost opt] # useradd-M-s / sbin/nologin nginx// Creator user nginx And limit its non-login terminal [root@localhost opt] # cd nginx-1.12.0/ [root@localhost nginx-1.12.0] #. / configure\ / configure nginx >-- prefix=//usr/local/nginx\ / / specify installation path >-- user=nginx\ / / specify user name >-- group=nginx\ / / specify the group to which the user belongs >-- with- Http_stub_status_module// installation status Statistics Module

5. Compilation and installation

[root@localhost nginx-1.12.0] # make & & make install

6. Optimize the Nginx service startup script and establish a command soft connection

[root@localhost nginx-1.12.0] # ln-s / usr/local/nginx/sbin/nginx / usr/local/sbin/ create nginx service command soft link to system command [root@localhost nginx-1.12.0] # systemctl stop firewalld.service / / turn off firewall [root@localhost nginx-1.12.0] # setenforce 0bat / turn off enhanced security function [root@localhost nginx-1.12.0] # nginx/ / input Nginx enables the service [root@localhost nginx-1.12.0] # netstat-ntap | grep 80 / / View port 80 of the service Shows that tcp 0 0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 01. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. master * LISTEN 7520/nginx:

1. Configure DNS domain name resolution service

[root@localhost ~] # yum-y install bind// installs the bind package of the DNS service [root@localhost ~] # vim / etc/named.conf / / Edit the main configuration file options {listen-on port 53 {any;}; # # replace the listening address 127.0.0.1 with any, listen-on-v6 port 53 {:: 1;}; directory "/ var/named" Dump-file "/ var/named/data/cache_dump.db"; statistics-file "/ var/named/data/named_stats.txt"; memstatistics-file "/ var/named/data/named_mem_stats.txt"; recursing-file "/ var/named/data/named.recursing"; secroots-file "/ var/named/data/named.secroots"; allow-query {any;} # # replace authorized localhost with any [root@localhost ~] # vim / etc/named.rfc1912.zones / / Edit the regional configuration file zone "kgc.com" IN {type master; file "kgc.com.zone"; allow-update {none;};}; zone "accp.com" IN {type master; file "accp.com.zone" Allow-update {none;};}; [root@localhost ~] # cd / var/named [root@localhost named] # cp-p named.localhost kgc.com.zone [root@localhost named] # cp-p named.localhost accp.com.zone [root@localhost named] # vim kgc.com.zone / / Edit the kgc domain name zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.235.158 IN SOA # delete the content of the last line and add the domain name resolution address to the local address [root@localhost named] # vim accp.com.zone / / Edit the accp domain name zone data configuration file $TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.235.158address # delete the content of the last line and add the domain name resolution address to the local address [root@localhost named] # systemctl start named / / enable the dns service [root@localhost named] # systemctl stop firewalld.service / / turn off the firewall [root@localhost named] # setenforce 0 / / disable the enhanced security feature

1. Configure virtual host

2. Create a self-test web page

[root@localhost named] # cd [root@localhost ~] # mkdir-p / var/www/html/kgc [root@localhost ~] # mkdir-p / var/www/html/accp [root@localhost ~] # ls / var/www/html/accp kgc [root@localhost ~] # cd / var/www/html/ [root@localhost html] # echo "this kgc web" > kgc/index.html [root@localhost html] # echo "this accp web" > accp/index.html

3. Edit the nginx.conf configuration file

[root@localhost html] # vim / usr/local/nginx/conf/nginx.confserver {listen 80; server_name www.kgc.com; charset utf-8; # # supports Chinese characters access_log logs/www.kgc.com.access.log; # # kgc site access log location / {} error_page 500502 503 504 / 50x.html # # Server error related webpage location = / 50x.html {root html;}} server {listen 80; server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log; location / {root / var/www/html/accp Index index.html index.htm;} error_page 500 502 503 504 / 50x.htl; location = / 50x.html {root html;}}

4. Reload Nginx service

[root@localhost ~] # killall-s HUP nginx [root@localhost ~] # netstat-ntap | grep 80tcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.

5. To test the web page, enter www. KGC. Com and www. Accp. Access to two domain names of com

Second, based on port

1. Configure virtual host

2. Create a test page for another port

[root@localhost ~] # cd / var/www/html/ [root@localhost html] # echo "this is kgc 8080 web" > kgc/index.html

3. Edit the nginx.conf configuration file and modify only the listening address

[root@localhost html] # vim / usr/local/nginx/conf/nginx.confserver {listen 192.168.235.158 listen 80; # # listen on port 80 server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log; location / {root / var/www/html/kgc; index index.html index.htm of the host } error_page 500502 503504 / 50x.htl; location = / 50x.html {root html;}} server {listen 192.168.235.158server 808080; # # listen on port 8080 server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log of the host Location / {root / var/www/html/kgc; index index.html index.htm;} error_page 500 502 503 504 / 50x.html; location = / 50x.html {root html;}}

4. Reload Nginx service

[root@localhost html] # killall-s HUP nginx [root@localhost html] # netstat-ntap | grep 80tcp 0 0 0.0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.

5. Test the web page and visit the default web page at port 80 and the web page at port 8080 respectively.

Third, based on IP

1. Add the network card and plan the IP

Host IP: 192.168.235.158; 192.168.235.142

Domain name: www. Kgc. Com; www. Accp. Com

2. Modify the configuration of the zone data file of the accp domain name

[root@localhost ~] # vim / var/named/accp.com.zone$TTL 1D @ IN SOA @ rname.invalid. (0; serial 1D; refresh 1H; retry 1W; expire 3H) Minimum NS @ A 127.0.0.1www IN A 192.168.235.142 systemctl restart named## # change the IP address to 192.168.235.142 [root@localhost ~] # restart the domain name resolution service

3. Edit the nginx.conf configuration file

[root@localhost ~] # vim / usr/local/nginx/conf/nginx.conf## this paragraph is not modified server {listen 192.168.235.158 listen 80; server_name www.kgc.com; charset utf-8; access_log logs/www.kgc.com.access.log; location / {root / var/www/html/kgc; index index.html index.htm } error_page 500502 503504 / 50x.htl; location = / 50x.html {root html;}} server {listen 192.168.235.142V80; # # modify the listening address of this section to 192.168.234.142 server_name www.accp.com; charset utf-8; access_log logs/www.accp.com.access.log Location / {root / var/www/html/accp; index index.html index.htm;} error_page 500 502 503 504 / 50x.html; location = / 50x.html {root html;}}

4. Reload Nginx service

[root@localhost ~] # killall-s HUP nginx [root@localhost ~] # netstat-ntap | grep 80tcp 0 192.168.235.142 netstat 80 0.0.0.0 LISTEN 7299/nginx: master

5. To test the web page, enter the IP address 192.168.235.158 and 192.168.235.142 respectively to access the web page.

After reading the appeal content, have you mastered the method of building nginx virtual host based on domain name, port and IP? If you want to know more related content, welcome to follow the industry information channel, thank you for reading!

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