日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置)

發(fā)布時(shí)間:2024/2/28 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

  • Nginx支持的虛擬主機(jī)有三種
  • 配置環(huán)境
    • 配置DNS域名
    • 安裝環(huán)境
    • 安裝nginx
    • 制作管理腳本
  • 基于域名
  • 基于端口
  • 基于IP
  • 基于用IP地址訪問域名

Nginx支持的虛擬主機(jī)有三種

●基于域名的虛擬主機(jī)
●基于IP的虛擬主機(jī)
●基于端口的虛擬主機(jī)
每一種虛擬主機(jī)均可通過“server{}" 配置段實(shí)現(xiàn)各自的功能

配置環(huán)境

配置DNS域名

root@localhost ~]# yum -y install bind [root@localhost ~]# vim /etc/named.conf options {listen-on port 53 { 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; };[root@localhost ~]# vim /etc/named.rfc1912.zones zone "benet.com" IN {type master;file "benet.com.zone";allow-update { none; }; }; zone "kgc.com" IN {type master;file "kgc.com.zone";allow-update { none; }; };[root@localhost ~]# cd /var/named/ [root@localhost named]# cp -p named.localhost benet.com.zone [root@localhost named]# vim benet.com.zone www IN A 192.168.136.10 [root@localhost named]# setenforce 0 [root@localhost named]# iptables -F [root@localhost named]# systemctl stop firewalld [root@localhost named]# systemctl start named

安裝環(huán)境

[root@localhost named]# yum -y install gcc gcc-c++ pcre-devel zlib-devel

安裝nginx

[root@localhost named]# tar zxvf nginx-1.12.2.tar.gz -C /opt/ [root@localhost opt]# cd nginx-1.12.2/ [root@localhost ~]# useradd -M -s /sbin/nologin nginx 編譯需要指定相關(guān)用戶 -M 創(chuàng)建家目錄 [root@localhost nginx-1.12.2]# ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_module [root@localhost nginx-1.12.2]# make && make install [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ [root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/conf/nginx.conf /etc/ [root@localhost nginx-1.12.2]# 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

制作管理腳本

[root@localhost nginx]# cd /etc/init.d/ 重建服務(wù) [root@localhost init.d]# vim nginx 創(chuàng)建管理腳本 #!/bin/bash # chkconfig: - 99 20 #description: nginx service Control Script PROG="/usr/local/nginx/sbin/nginx" PIDF="/usr/local/nginx/logs/nginx.pid" case "$1" instart)$PROG;;stop)kill -s QUIT $(cat $PIDF);;restart)$0 stop$0 start;;reload)kill -s HUP $(cat $PIDF);;*)echo "Usage: $0 {start|stop|restart|reload}"exit 1esac exit 0 [root@localhost init.d]# chmod +x nginx [root@localhost init.d]# chkconfig --add nginx 便于servers管理 [root@promote init.d]# service nginx start [root@localhost init.d]# netstat -ntap | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25954/nginx: master tcp 0 0 192.168.136.10:22 192.168.136.2:59246 ESTABLISHED 8049/sshd: root@pts tcp 0 0 192.168.136.10:48214 59.111.0.251:80 TIME_WAIT -

基于域名

創(chuàng)建虛擬主機(jī)

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf在末尾添加 server {server_name www.kgc.com;location / {root /var/www/kgc;index index.html index.php;}}server {server_name www.benet.com;location / {root /var/www/benet;index index.html index.php;}}

開啟服務(wù)

[root@localhost conf]# 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

寫入網(wǎng)頁

[root@localhost ~]# mkdir -p /var/www/html/kgc [root@localhost ~]# mkdir -p /var/www/html/benet [root@localhost html]# echo "this is kgc web" > kgc/index.html [root@localhost html]# echo "this is benet web" > benet/index.html [root@localhost conf]# service nginx restart

基于端口

[root@localhost html]# cd /usr/local/nginx/conf/ [root@localhost conf]# vim nginx.confvim /etc/nginx.confserver {listen 192.168.60.60:80;server_name www.kgc.com;location / {root /var/www/html/kgc;index index.html index.htm; } }server {listen 192.168.60.60:8080;server_name www.kgc.com;location / {root /var/www/html/benet8080;index index.html index.htm; } }

寫入網(wǎng)頁

[root@localhost conf]# cd /var/www//html/ [root@localhost html]# mkdir benet8080 [root@localhost html]# echo "this is benet8080 web" > benet8080/index.html [root@localhost conf]# 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

基于IP

添加一個(gè)網(wǎng)卡 左邊是IP地址查看是否可以通信

配置DNS區(qū)域配置文件

[root@localhost named]# vim kgc.com.zone www IN A 192.168.136.136 [root@localhost named]# systemctl restart named

在win10中查看一下解析地址

修改一下kgc的ip地址

[root@localhost named]# cd /usr/local/nginx/conf/ [root@localhost conf]# vim nginx.conf vim /etc/nginx.confserver {listen 192.168.136.136:80;server_name www.kgc.com;location / {root /var/www/html/kgc;index index.html index.htm; } }server {listen 192.168.136.10:80;server_name www.benet.com;location / {root /var/www/html/benet;index index.html index.htm;} }

驗(yàn)證一下是否成功并開啟

[root@localhost conf]# 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 [root@localhost conf]# service nginx restart

基于用IP地址訪問域名

[root@localhost conf]# vim nginx.conf

開啟服務(wù)

[root@localhost conf]# service nginx restart

開一臺(tái)win7的服務(wù)器

解析一下地址

網(wǎng)頁查看一下

win10的網(wǎng)頁查看

結(jié)論:當(dāng)我們配置沒有問題時(shí)候可以找別的方法去解決,比如換一臺(tái)客戶終端測試(可能是客戶端的問題)

總結(jié)

以上是生活随笔為你收集整理的nginx虚拟主机(基于域名虚拟主机、基于IP地址虚拟主机、基于端口虚拟主机设置)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。