學(xué)習(xí)完整課程請移步 互聯(lián)網(wǎng) Java 全棧工程師
本節(jié)視頻
概述
我們使用 Docker 來安裝和運行 Nginx,docker-compose.yml 配置如下:
version: '3.1'
services:
nginx:
restart: always
image: nginx
container_name: nginx
ports:
- 81:80
volumes:
- ./conf/nginx.conf:/etc/nginx/nginx.conf
- ./wwwroot:/usr/share/nginx/wwwroot
什么是虛擬主機?
虛擬主機是一種特殊的軟硬件技術(shù),它可以將網(wǎng)絡(luò)上的每一臺計算機分成多個虛擬主機,每個虛擬主機可以獨立對外提供 www 服務(wù),這樣就可以實現(xiàn)一臺主機對外提供多個 web 服務(wù),每個虛擬主機之間是獨立的,互不影響的。
通過 Nginx 可以實現(xiàn)虛擬主機的配置,Nginx 支持三種類型的虛擬主機配置
- 基于 IP 的虛擬主機
- 基于域名的虛擬主機
- 基于端口的虛擬主機
Nginx 配置文件的結(jié)構(gòu)
# ...
events {
# ...
}
http {
# ...
server{
# ...
}
# ...
server{
# ...
}
}
注: 每個 server 就是一個虛擬主機
基于端口的虛擬主機配置
需求
- Nginx 對外提供 80 和 8080 兩個端口監(jiān)聽服務(wù)
- 請求 80 端口則請求 html80 目錄下的 html
- 請求 8080 端口則請求 html8080 目錄下的 html
創(chuàng)建目錄及文件
在 /usr/local/docker/nginx/wwwroot 目錄下創(chuàng)建 html80 和 html8080 兩個目錄,并分辨創(chuàng)建兩個 index.html 文件
配置虛擬主機
修改 /usr/local/docker/nginx/conf 目錄下的 nginx.conf 配置文件:
# 啟動進程,通常設(shè)置成和 CPU 的數(shù)量相等
worker_processes 1;
events {
# epoll 是多路復(fù)用 IO(I/O Multiplexing) 中的一種方式
# 但是僅用于 linux2.6 以上內(nèi)核,可以大大提高 nginx 的性能
use epoll;
# 單個后臺 worker process 進程的最大并發(fā)鏈接數(shù)
worker_connections 1024;
}
http {
# 設(shè)定 mime 類型,類型由 mime.type 文件定義
include mime.types;
default_type application/octet-stream;
# sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件,對于普通應(yīng)用,
# 必須設(shè)為 on,如果用來進行下載等應(yīng)用磁盤 IO 重負載應(yīng)用,可設(shè)置為 off,以平衡磁盤與網(wǎng)絡(luò) I/O 處理速度,降低系統(tǒng)的 uptime.
sendfile on;
# 連接超時時間
keepalive_timeout 65;
# 設(shè)定請求緩沖
client_header_buffer_size 2k;
# 配置虛擬主機 192.168.75.145
server {
# 監(jiān)聽的ip和端口,配置 192.168.75.145:80
listen 80;
# 虛擬主機名稱這里配置ip地址
server_name 192.168.75.145;
# 所有的請求都以 / 開始,所有的請求都可以匹配此 location
location / {
# 使用 root 指令指定虛擬主機目錄即網(wǎng)頁存放目錄
# 比如訪問 http://ip/index.html 將找到 /usr/local/docker/nginx/wwwroot/html80/index.html
# 比如訪問 http://ip/item/index.html 將找到 /usr/local/docker/nginx/wwwroot/html80/item/index.html
root /usr/share/nginx/wwwroot/html80;
# 指定歡迎頁面,按從左到右順序查找
index index.html index.htm;
}
}
# 配置虛擬主機 192.168.75.245
server {
listen 8080;
server_name 192.168.75.145;
location / {
root /usr/share/nginx/wwwroot/html8080;
index index.html index.htm;
}
}
}
基于域名的虛擬主機配置
需求
- 兩個域名指向同一臺 Nginx 服務(wù)器,用戶訪問不同的域名顯示不同的網(wǎng)頁內(nèi)容
- 兩個域名是 admin.service.itoken.funtl.com 和 admin.web.itoken.funtl.com
- Nginx 服務(wù)器使用虛擬機 192.168.75.145
配置 Windows Hosts 文件
- 通過 host 文件指定 admin.service.itoken.funtl.com 和 admin.web.itoken.funtl.com 對應(yīng) 192.168.75.145 虛擬機:
- 修改 window 的 hosts 文件:(C:\Windows\System32\drivers\etc)
創(chuàng)建目錄及文件
在 /usr/local/docker/nginx/wwwroot 目錄下創(chuàng)建 htmlservice 和 htmlweb 兩個目錄,并分辨創(chuàng)建兩個 index.html 文件
配置虛擬主機
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name admin.service.itoken.funtl.com;
location / {
root /usr/share/nginx/wwwroot/htmlservice;
index index.html index.htm;
}
}
server {
listen 80;
server_name admin.web.itoken.funtl.com;
location / {
root /usr/share/nginx/wwwroot/htmlweb;
index index.html index.htm;
}
}
}