Nginx configuration - virtual host is based on IP, domain name and port (actual combat!)

Nginx virtual host Domain name based virtual host Virtual host based on IP address Port based virtual host 1. Install DNS domain name resolution serv...
Nginx virtual host
  • Domain name based virtual host
  • Virtual host based on IP address
  • Port based virtual host

1. Install DNS domain name resolution server

1. Install bind server
[root@localhost ~]# yum install bind -y
2. Modify the main configuration file (named.conf)
[root@localhost ~]# vim /etc/named.conf options { listen-on port 53 { any; }; ##Monitor all 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; }; ##Allow all
3. Modify the region configuration file (named.rfc1912.zones)
[root@localhost ~]# vim /etc/named.rfc1912.zones ##Configure zone profiles zone "kgc.com" IN { type master; file "kgc.com.zone"; ##kgc area data profile allow-update { none; }; }; zone "accp.com" IN { type master; file "accp.com.zone"; ##accp area data profile allow-update { none; }; };
4. Modify the region data configuration file (KGC. Com. Zone ACCP. Com. Zone)
[root@localhost ~]# cd /var/named/ [root@localhost named]# cp -p named.localhost kgc.com.zone ##Copy template [root@localhost named]# vim kgc.com.zone ##Modify zone profile $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.13.128 ##Native address [root@localhost named]# cp -p kgc.com.zone accp.com.zone ##Copy file as accp area data configuration file [root@localhost named]# systemctl start named ##Start dns Service [root@localhost named]# systemctl stop firewalld.service ##Turn off firewall [root@localhost named]# setenforce 0
5. Create the site directory of two websites and add the content of the first page
[root@localhost ~]# mkdir -p /var/www/html/accp ##Create accp site [root@localhost ~]# mkdir -p /var/www/html/kgc ##Create kgc site [root@localhost ~]# cd /var/www/html/ [root@localhost html]# ls accp kgc [root@localhost html]# echo "this a accp web" > accp/index.html ##Create homepage content [root@localhost html]# echo "this a kgc web" > kgc/index.html ##Create homepage content

Second, share the required compression package of LAMP on Windows (if you have any questions here, please refer to the previous blog articles)

Third, use remote share to get files and mount them to mnt directory on Linux

[root@localhost ~]# smbclient -L //192.168.100.3 / ා񖓿񖓿ාremote shared access Enter SAMBA\root's password: Sharename Type Comment --------- ---- ------- LNMP-C7 Disk [root@localhost ~]# mount.cifs //192.168.100.3/LNMP-C7 /mnt ××××××××× mount to / mnt directory

4. Compile and install Nginx

1. Unzip the source package to / opt and check
[root@localhost ~]# cd /mnt ##Switch to mount point directory [root@localhost mnt]# ls Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz [root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt ##Extract the Nginx source package to / opt [root@localhost mnt]# cd /opt/ ##Switch to the unzipped directory [root@localhost opt]# ls nginx-1.12.2 rh
2. Install the environment component package required for compilation
[root@localhost opt]# yum -y install \ gcc \ //c language gcc-c++ \ //c++ language pcre-devel \ //pcre language tools zlib-devel //Function library for data compression
3. Create program user nginx and compile nginx
[root@localhost opt]# useradd -M -s /sbin/nologin nginx ##Create program user, safe and non login status [root@localhost opt]# id nginx uid=1001(nginx) gid=1001(nginx) group=1001(nginx) [root@localhost opt]# cd nginx-1.12.0/ ##Switch to nginx directory [root@localhost nginx-1.12.0]# ./configure \ ##Configure nginx > --prefix=/usr/local/nginx \ ##Installation path > --user=nginx \ ##User name > --group=nginx \ ##User group > --with-http_stub_status_module ##Status statistics module
4. Compile and install
[root@localhost nginx-1.12.0]# make ##Compile ... [root@localhost nginx-1.12.0]# make install ##install ...
5. Optimize nginx startup script for system identification
[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##Create a soft connection for the system to recognize the nginx startup script [root@localhost nginx]# nginx -t ##Check configuration file for syntax problems 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 [root@localhost nginx]# nginx ##Open ngnix [root@localhost nginx]# netstat -ntap | grep 80 ##Check the port. nginx is on tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 39620/nginx: master [root@localhost nginx]# systemctl stop firewalld.service ##Turn off firewall [root@localhost nginx]# setenforce 0 [root@localhost nginx]# nginx ##open
6. Make management script for service management
[root@localhost nginx]# cd /etc/init.d/ ##Switch to startup profile directory [root@localhost init.d]# ls functions netconsole network README [root@localhost init.d]# vim nginx ##Edit startup script file #!/bin/bash # chkconfig: - 99 20 ##Annotation information # description: Nginx Service Control Script PROG="/usr/local/nginx/sbin/nginx" ##Set variable to nginx command file PIDF="/usr/local/nginx/logs/nginx.pid" ##Set the variable PID file process number to 5346 case "$1" in start) $PROG ##Opening service ;; stop) kill -s QUIT $(cat $PIDF) ##Shut down service ;; restart) ##Restart service $0 stop $0 start ;; reload) ##Heavy load service kill -s HUP $(cat $PIDF) ;; *) ##Error input prompt echo "Usage: $0 " exit 1 esac exit 0 [root@localhost init.d]# chmod +x /etc/init.d/nginx ##Give startup script execution permission [root@localhost init.d]# chkconfig --add nginx ##Add to service manager [root@localhost init.d]# service nginx stop ##You can use service to control nginx [root@localhost init.d]# service nginx start
7. It is also convenient for system CTL management and configuration file (just write one for convenience)
[root@localhost ~]# vim /lib/systemd/system/nginx.service ##create profile [Unit] Description=nginx ##describe After=network.target ##Describe service type [Service] Type=forking ##Background operation form PIDFile=/usr/local/nginx/logs/nginx.pid ##PID file location ExecStart=/usr/local/nginx/sbin/nginx ##Startup service ExecReload=/usr/bin/kill -s HUP $MAINPID ##Overload configuration according to PID ExecStop=/usr/bin/kill -s QUIT $MAINPID ##Terminate process according to PID PrivateTmp=true [Install] WantedBy=multi-user.target [root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service ##Set execution permission [root@localhost ~]# systemctl stop nginx.service ##Close [root@localhost ~]# systemctl start nginx.service ##open

V. virtual host based on different domain names

1. Modify nginx configuration file information
[root@localhost ~]# cd /usr/local/nginx/conf [root@localhost conf]# vim nginx.conf ##Modify Nginx configuration file server { listen 80; server_name www.kgc.com; ##kgc website charset utf-8; access_log logs/www.kgc.com.access.log; ##log file location / { root /var/www/html/kgc; ##Site directory index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.accp.com; ##accp website charset utf-8; access_log logs/www.accp.com.access.log; ##log file location / { root /var/www/html/accp; ##Site directory index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } [root@localhost conf]# service nginx restart ##Restart nginx service
2. Use the tester to visit websites with different domain names


Vi. virtual host based on different ports

1. Modify nginx configuration file information
[root@localhost ~]# cd /usr/local/nginx/conf [root@localhost conf]# vim nginx.conf ##Modify Nginx configuration file 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.html; location = /50x.html { root html; } } server { listen 192.168.13.138:8080; ##Modify listening port to 8080 server_name www.accp.com; charset utf-8; access_log logs/www.accp8080.com.access.log; ##Log file changed to 8080 location / { root /var/www/html/accp8080; ##Site directory for port 8080 index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
2. Create the directory of accp8080 site and the content of the first page
[root@localhost conf]# cd /var/www/html/ ##Switch to site directory [root@localhost html]# mkdir accp8080 ##Create site directory [root@localhost html]# cd accp8080/ [root@localhost accp8080]# echo "this is accp8080 web" > index.html ##Create homepage content [root@localhost accp8080]# service nginx restart ##Restart Nginx service
3. Use the tester to visit websites on different ports


VII. Virtual hosts based on different IP addresses

1. Add a network card to the virtual machine 192.168.13.138 192.168.13.133

2. Modify dns area data configuration file
[root@localhost ~]# cd var/named/ [root@localhost named]# vim kgc.com.zone ##Modify the area data configuration file of kgc $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.13.133 ##The address is 133. [root@localhost named]# vim accp.com.zone ##Modify the area data configuration file of accp $TTL 1D @ IN SOA @ rname.invalid. ( 0 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum NS @ A 127.0.0.1 www IN A 192.168.13.138 ##The address is 138. [root@localhost named]# systemctl restart named ##Restart dns Service
3. Modify nginx configuration file information
[root@localhost ~]# cd /usr/local/nginx/conf [root@localhost conf]# vim nginx.conf ##Modify Nginx configuration file server { listen 192.168.13.133:80; ##specify an IP address 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 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.13.138:80; ##specify an IP address 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; } } [root@localhost conf]# service nginx restart ##Restart Nginx service
4. Use the tester to visit different IP websites


Thank you for reading!

4 November 2019, 19:32 | Views: 2971

Add new comment

For adding a comment, please log in
or create account

0 comments