時間:2023-07-17 13:39:01 | 來源:網站運營
時間:2023-07-17 13:39:01 來源:網站運營
nginx基于域名、ip或者端口的虛擬主機配置:1.首先先關閉防火墻和selinux,然后測試nginx是否能正常啟動,網頁是否能訪問到。2.基于域名的虛擬主機配置通過域名區(qū)分的虛擬機[root@localhost nginx-1.16.1]# vim /etc/nginx/nginx.conf #進入配置文件然后找到server并進行如下操作
#根據(jù)圖去修改 server { listen 80; server_name www.testhtml.com; location / { root /var/www/first_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name web.testhtml.com; location / { root /var/www/second_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
創(chuàng)建目錄和index文件,并寫入數(shù)據(jù)進行測試[root@localhost nginx-1.16.1]# mkdir -p /var/www/first_test/[root@localhost nginx-1.16.1]# vim /var/www/first_test/index.html[root@localhost nginx-1.16.1]# mkdir -p /var/www/second_test/[root@localhost nginx-1.16.1]# vim /var/www/second_test/index.html測試用[root@localhost nginx-1.16.1]# cat /var/www/first_test/index.html #查看我寫的數(shù)據(jù)this is first html[root@localhost nginx-1.16.1]# cat /var/www/second_test/index.htmlthis is second html
重新加載配置文件# 如果編譯安裝的執(zhí)行[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -s reload# 如果 yum 安裝的執(zhí)行[root@localhost nginx-1.16.1]# nginx -s reload
因為網址是我們自定義的,所以我們需要用windows解析一下客戶端配置解析 在 C:/Windows/System32/drivers/etc/hosts 文件中添加兩行192.168.13.129 web.testhtml.com 192.168.13.129 www.testhtml.com#ip地址是你正在使用的有nginx的虛擬機地址
記得一定要保存瀏覽器輸入:http://www.testhtml.com
瀏覽器輸入:http://web.testhtml.com
3.基于ip的虛擬主機 首先我們要配置一個子接口[root@localhost ~]# ifconfig ens33:1 192.168.13.139/24 #自己網段的ip[root@localhost ~]# ip a #查看1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ed:a9:e9 brd ff:ff:ff:ff:ff:ff inet 192.168.13.129/24 brd 192.168.13.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.13.139/24 brd 192.168.13.255 scope global secondary ens33:1 valid_lft forever preferred_lft forever inet6 fe80::bf48:52fa:e308:53f4/64 scope link valid_lft forever preferred_lft forever
配置通過ip區(qū)分的虛擬機#步驟跟基于域名的虛擬機配置差不多,只需要將server_name 后面的變?yōu)閕p地址就行了,而且也不用去解析server { listen 80; server_name 192.168.13.129; location / { root /var/www/first_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name 192.168.13.139; location / { root /var/www/second_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
重新加載配置文件# 如果編譯安裝的執(zhí)行[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload# 如果 yum 安裝的執(zhí)行[root@localhost ~]# nginx -s reload
測試訪問瀏覽器輸入:http://192.168.13.129
瀏覽器輸入:http://192.168.13.139
-- 刪除綁定的臨時ip[root@localhost ~]# ifconfig ens33:1 192.168.13.139/24 down重啟一下nginx[root@localhost ~]# systemctl restart nginx#同一個ip,但是端口號不同 server { listen 80; server_name 192.168.13.129; location / { root /var/www/first_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 8080; server_name 192.168.13.129; location / { root /var/www/second_test/; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
重新加載配置文件# 如果編譯安裝的執(zhí)行[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload# 如果 yum 安裝的執(zhí)行[root@localhost ~]# nginx -s reload
測試訪問瀏覽器輸入:http://192.168.13.129瀏覽器輸入:http://192.168.13.129:8080
你會看到跟之前一樣的網頁你們的評論和點贊是我寫文章的最大動力,蟹蟹。
關鍵詞:虛擬,配置,主機