欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Nginx 虛擬主機配置的三種方式(基于端口)

 更新時間:2018年10月30日 10:50:34   作者:B8613A  
Nginx配置虛擬主機支持3種方式主要有基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。本篇文章主要介紹了基于端口的實現(xiàn),感興趣的小伙伴們可以參考一下

Nginx配置虛擬主機支持3種方式:基于IP的虛擬主機配置,基于端口的虛擬主機配置,基于域名的虛擬主機配置。

詳解Nginx 虛擬主機配置的三種方式(基于IP) http://www.dbjr.com.cn/article/14974.htm

詳解Nginx 虛擬主機配置的三種方式(基于域名) http://www.dbjr.com.cn/article/14978.htm

2、Nginx基于端口的虛擬主機配置

如一臺服務(wù)器只有一個IP或需要通過不同的端口訪問不同的虛擬主機,可以使用基于端口的虛擬主機配置。

2.1 假設(shè)服務(wù)器有個IP地址為192.168.2.154

[root@localhost conf]# ifconfig ens33:4 192.168.2.154/24 up
[root@localhost conf]# ifconfig
ens33:4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.154 netmask 255.255.255.0 broadcast 192.168.2.255
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

2.2 需要配置的虛擬主機分別為7081、8081和9081,配置主機的host文件便于測試。

[root@localhost conf]# vim /etc/hosts
[root@localhost conf]# cat /etc/hosts|grep 192.168.2.154
192.168.2.154 www.test154.com

2.3 建立虛擬主機存放網(wǎng)頁的根目錄,并創(chuàng)建首頁文件index.html

[root@localhost conf]# cd /data/www/
[root@localhost www]# mkdir port
[root@localhost www]# cd port/
[root@localhost port]# mkdir 7081 8081 9081
[root@localhost port]# ls
7081 8081 9081
[root@localhost port]# echo "port 7081" > 7081/index.html
[root@localhost port]# echo "port 8081" > 8081/index.html
[root@localhost port]# echo "port 9081" > 9081/index.html

2.4 修改nginx.conf,將虛擬主機配置文件包含進主文件

[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf  fastcgi_params  koi-utf mime.types  nginx.conf  scgi_params  uwsgi_params  win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost conf]# vim nginx.conf

在nginx.conf文件末尾加入以下配置

# 在http段中找到以下內(nèi)容并刪除每行前面的“#”
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
   '$status $body_bytes_sent "$http_referer" '
   '"$http_user_agent" "$http_x_forwarded_for"';

# 配置文件結(jié)尾的最后一個“}”之前加入以下語句,如下所示
include vhost/*.conf

2.5 編輯每個端口的配置文件

[root@localhost vhost]# vim www.test154.7081.conf
[root@localhost vhost]# cat www.test154.7081.conf
 server {
 listen 192.168.2.154:7081;
 # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.7081.log main;
 error_log /data/logs/www.test154.7081.error.log;

 location / {
  root /data/www/port/7081;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# vim www.test154.8081.conf
[root@localhost vhost]# cat www.test154.8081.conf
 server {
 listen 192.168.2.154:8081;
 # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.8081.log main;
 error_log /data/logs/www.test154.8081.error.log;

 location / {
  root /data/www/port/8081;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# vim www.test154.9081.conf
[root@localhost vhost]# cat www.test154.9081.conf
 server {
 listen 192.168.2.154:9081;
 # 配置成實際的域名,每個虛擬主機的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.9081.log main;
 error_log /data/logs/www.test154.9081.error.log;

 location / {
  root /data/www/port/9081;
  index index.html index.htm;
 }
 }

2.6 創(chuàng)建日志文件,否則無法啟動nginx

[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.test154.7081.log
[root@localhost /]# touch /data/logs/www.test154.7081.error.log
[root@localhost /]# touch /data/logs/www.test154.8081.log
[root@localhost /]# touch /data/logs/www.test154.8081.error.log
[root@localhost /]# touch /data/logs/www.test154.9081.log
[root@localhost /]# touch /data/logs/www.test154.9081.error.log
[root@localhost /]# ls /data/logs/
www.test154.7081.error.log www.test154.8081.error.log www.test154.9081.error.log
www.test154.7081.log www.test154.8081.log www.test154.9081.log

2.7 先測試配置文件然后再啟動nginx

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./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
# 啟動nginx
[root@localhost sbin]# ./nginx

2.8 測試文件

[root@localhost ~]# curl http://www.test154.com:7081
port 7081
[root@localhost ~]# curl http://www.test154.com:8081
port 8081
[root@localhost ~]# curl http://www.test154.com:9081
port 9081

附:配置過程中的問題

1、最后測試時發(fā)生的問題

[root@localhost sbin]# curl http://www.test154.com:7081
curl: (7) Failed connect to www.test154.com:7081; 拒絕連接
[root@localhost sbin]# curl 192.168.2.154:7081
curl: (7) Failed connect to 192.168.2.154:7081; 拒絕連接

解決方法:

1.1 使用以下命令查看Nginx是否在監(jiān)聽相應(yīng)的端口

[root@localhost conf]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State
tcp 0 0 0.0.0.0:111  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.153:80 0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.152:80 0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.151:80 0.0.0.0:*  LISTEN
tcp 0 0 0.0.0.0:8080  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:8081 0.0.0.0:*  LISTEN
tcp 0 0 0.0.0.0:22  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:9081 0.0.0.0:*  LISTEN
tcp 0 0 127.0.0.1:25  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:7081 0.0.0.0:*  LISTEN
tcp6 0 0 :::111   :::*   LISTEN
tcp6 0 0 :::22   :::*   LISTEN
tcp6 0 0 :::23   :::*   LISTEN
tcp6 0 0 ::1:25   :::*   LISTEN

1.2 若Nginx未監(jiān)聽相應(yīng)端口則重啟Nginx服務(wù),再不行重啟服務(wù)器

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Nginx配置優(yōu)化詳解

    Nginx配置優(yōu)化詳解

    如果你已經(jīng)安裝過Nginx并在生產(chǎn)環(huán)境中使用,那么Nginx配置優(yōu)化你一定也要做,這樣才能看到Nginx性能,本文就從基本配置優(yōu)化開始到高層配置教你如何優(yōu)化Nginx
    2013-11-11
  • LINUX中NGINX反向代理下的TOMCAT集群(詳解)

    LINUX中NGINX反向代理下的TOMCAT集群(詳解)

    下面小編就為大家?guī)硪黄狶INUX中NGINX反向代理下的TOMCAT集群(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-05-05
  • Nginx服務(wù)器限制IP訪問的各種情況全解析

    Nginx服務(wù)器限制IP訪問的各種情況全解析

    這篇文章主要介紹了Nginx服務(wù)器限制IP訪問的各種情況全解析,包括限制同一IP在一段時間內(nèi)的訪問次數(shù)和全局限IP訪問以及限制IP訪問指定目錄等情況,需要的朋友可以參考下
    2015-08-08
  • 詳解Nginx中HTTP的keepalive相關(guān)配置

    詳解Nginx中HTTP的keepalive相關(guān)配置

    這篇文章主要介紹了Nginx中HTTP的keepalive相關(guān)配置,以及Nginx的Httpd守護進程相關(guān)的keepalive timeout配置,需要的朋友可以參考下
    2016-01-01
  • Nginx配置支持WebSocket功能詳解

    Nginx配置支持WebSocket功能詳解

    Nginx配置支持WebSocket功能需要添加特定配置,網(wǎng)上通用配置只能支持ws請求,而既支持http又支持ws的配置中,使用map$http_upgrade$connection_upgrade塊來設(shè)置Connection頭的值,并指定使用HTTP/1.1版本以保持連接打開,確保Nginx版本是1.3或更高
    2024-11-11
  • 通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源

    通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源

    這篇文章為大家詳細主要介紹了如何通過nginx實現(xiàn)訪問服務(wù)器指定目錄下圖片資源,文中通過圖文進行了詳細的講解,有需要的小伙伴可以了解下
    2023-10-10
  • Nginx負載均衡/SSL配置的實現(xiàn)

    Nginx負載均衡/SSL配置的實現(xiàn)

    這篇文章主要介紹了Nginx負載均衡/SSL配置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-10-10
  • 關(guān)于多級緩存使用(nginx本地緩存、JVM進程緩存、redis緩存)

    關(guān)于多級緩存使用(nginx本地緩存、JVM進程緩存、redis緩存)

    這篇文章主要介紹了關(guān)于多級緩存使用(nginx本地緩存、JVM進程緩存、redis緩存),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • nginx配置SSL證書實現(xiàn)https服務(wù)的方法

    nginx配置SSL證書實現(xiàn)https服務(wù)的方法

    這篇文章主要介紹了nginx配置SSL證書實現(xiàn)https服務(wù)的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • Nginx如何封禁IP和IP段的實現(xiàn)

    Nginx如何封禁IP和IP段的實現(xiàn)

    這篇文章主要介紹了Nginx如何封禁IP和IP段的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07

最新評論