ginx靜態資源的配置指令
listen指令
listen:用來配置監聽端口。
語法 | listen address[:port] [default_server]...; listen port [default_server]...; |
默認值 | listen *:80 | *:8000 |
位置 | server |
listen的設置比較靈活,我們通過幾個例子來把常用的設置方式熟悉下:
listen 127.0.0.1:8000; // listen localhost:8000 監聽指定的IP和端口
listen 127.0.0.1; 監聽指定IP的所有端口
listen 8000; 監聽指定端口上的連接
listen *:8000; 監聽指定端口上的連接
default_server屬性是標識符,用來將此虛擬主機設置成默認主機。所謂的默認主機指的是如果沒有匹配到對應的address:port,則會默認執行的。如果不指定默認使用的是第一個server。
server{
listen 8080;
server_name 127.0.0.1;
location /{
root html;
index index.html;
}
}
server{
listen 8080 default_server;
server_name localhost;
default_type text/plain;
return 444 'This is a error request';
}
server_name指令
server_name:用來設置虛擬主機服務名稱。
127.0.0.1 、 localhost 、域名[www.baidu.com | www.jd.com]
語法 | server_name name ...; name可以提供多個中間用空格分隔 |
默認值 | server_name ""; |
位置 | server |
關于server_name的配置方式有三種,分別是:
·精確匹配
·通配符匹配
·正則表達式匹配
配置方式一:精確匹配
如
server {
listen 80;
server_name www.itcast.cn www.itheima.cn;
...
}
補充小知識點:
hosts是一個沒有擴展名的系統文件,可以用記事本等工具打開,其作用就是將一些常用的網址域名與其對應的IP地址建立一個關聯“數據庫”,當用戶在瀏覽器中輸入一個需要登錄的網址時,系統會首先自動從hosts文件中尋找對應的IP地址,一旦找到,系統會立即打開對應網頁,如果沒有找到,則系統會再將網址提交DNS域名解析服務器進行IP地址的解析。
windows:C:\Windows\System32\drivers\etc
centos:/etc/hosts
因為域名是要收取一定的費用,所以我們可以使用修改hosts文件來制作一些虛擬域名來使用。需要修改 /etc/hosts文件來添加
vim /etc/hosts
127.0.0.1 www.itcast.cn
127.0.0.1 www.itheima.cn
配置方式二:使用通配符配置
server_name中支持通配符"*",但需要注意的是通配符不能出現在域名的中間,只能出現在首段或尾段,如:
server {
listen 80;
server_name *.itcast.cn www.itheima.*;
# www.itcast.cn abc.itcast.cn www.itheima.cn www.itheima.com
...
}
下面的配置就會報錯
server {
listen 80;
server_name www.*.cn www.itheima.c*
...
}
配置三:使用正則表達式配置
server_name中可以使用正則表達式,并且使用~作為正則表達式字符串的開始標記。
常見的正則表達式
代碼 | 說明 |
^ | 匹配搜索字符串開始位置 |
$ | 匹配搜索字符串結束位置 |
. | 匹配除換行符\n之外的任何單個字符 |
\ | 轉義字符,將下一個字符標記為特殊字符 |
[xyz] | 字符集,與任意一個指定字符匹配 |
[a-z] | 字符范圍,匹配指定范圍內的任何字符 |
\w | 與以下任意字符匹配 A-Z a-z 0-9 和下劃線,等效于[A-Za-z0-9_] |
\d | 數字字符匹配,等效于[0-9] |
{n} | 正好匹配n次 |
{n,} | 至少匹配n次 |
{n,m} | 匹配至少n次至多m次 |
* | 零次或多次,等效于{0,} |
+ | 一次或多次,等效于{1,} |
? | 零次或一次,等效于{0,1} |
配置如下:
server{
listen 80;
server_name ~^www\.(\w+)\.com$;
default_type text/plain;
return 200 $1 $2 ..;
}
注意 ~后面不能加空格,括號可以取值
匹配執行順序
由于server_name指令支持通配符和正則表達式,因此在包含多個虛擬主機的配置文件中,可能會出現一個名稱被多個虛擬主機的server_name匹配成功,當遇到這種情況,當前的請求交給誰來處理呢?
server{
listen 80;
server_name ~^www\.\w+\.com$;
default_type text/plain;
return 200 'regex_success';
}
server{
listen 80;
server_name www.itheima.*;
default_type text/plain;
return 200 'wildcard_after_success';
}
server{
listen 80;
server_name *.itheima.com;
default_type text/plain;
return 200 'wildcard_before_success';
}
server{
listen 80;
server_name www.itheima.com;
default_type text/plain;
return 200 'exact_success';
}
server{
listen 80 default_server;
server_name _;
default_type text/plain;
return 444 'default_server not found server';
}
結論:
exact_success
wildcard_before_success
wildcard_after_success
regex_success
default_server not found server!!
No1:準確匹配server_name
No2:通配符在開始時匹配server_name成功
No3:通配符在結束時匹配server_name成功
No4:正則表達式匹配server_name成功
No5:被默認的default_server處理,如果沒有指定默認找第一個server
location指令
server{
listen 80;
server_name localhost;
location / {
}
location /abc{
}
...
}
location:用來設置請求的URI
默認值 | — |
語法 | location [ = | ~ | ~* | ^~ |@ ] uri{...} |
位置 | server,location |
uri變量是待匹配的請求字符串,可以不包含正則表達式,也可以包含正則表達式,那么nginx服務器在搜索匹配location的時候,是先使用不包含正則表達式進行匹配,找到一個匹配度最高的一個,然后在通過包含正則表達式的進行匹配,如果能匹配到直接訪問,匹配不到,就使用剛才匹配度最高的那個location來處理請求。
屬性介紹:
不帶符號,要求必須以指定模式開始
server {
listen 80;
server_name 127.0.0.1;
location /abc{
default_type text/plain;
return 200 "access success";
}
}
以下訪問都是正確的
http://192.168.200.133/abc
http://192.168.200.133/abc?p1=TOM
http://192.168.200.133/abc/
http://192.168.200.133/abcdef
= : 用于不包含正則表達式的uri前,必須與指定的模式精確匹配
server {
listen 80;
server_name 127.0.0.1;
location =/abc{
default_type text/plain;
return 200 "access success";
}
}
可以匹配到
http://192.168.200.133/abc
http://192.168.200.133/abc?p1=TOM
匹配不到
http://192.168.200.133/abc/
http://192.168.200.133/abcdef
~ : 用于表示當前uri中包含了正則表達式,并且區分大小寫
~*: 用于表示當前uri中包含了正則表達式,并且不區分大小寫
換句話說,如果uri包含了正則表達式,需要用上述兩個符合來標識
server {
listen 80;
server_name 127.0.0.1;
location ~^/abc\w${
default_type text/plain;
return 200 "access success";
}
}
server {
listen 80;
server_name 127.0.0.1;
location ~*^/abc\w${
default_type text/plain;
return 200 "access success";
}
}
^~: 用于不包含正則表達式的uri前,功能和不加符號的一致,唯一不同的是,如果模式匹配,那么就停止搜索其他模式了。
server {
listen 80;
server_name 127.0.0.1;
location ^~/abc{
default_type text/plain;
return 200 "access success";
}
}
設置請求資源的目錄root / alias
root:設置請求的根目錄
語法 | root path; |
默認值 | root html; |
位置 | http、server、location |
path為Nginx服務器接收到請求以后查找資源的根目錄路徑。
alias:用來更改location的URI
語法 | alias path; |
默認值 | — |
位置 | location |
path為修改后的根路徑。
以上兩個指令都可以來指定訪問資源的路徑,那么這兩者之間的區別是什么?
舉例說明:
(1)在/usr/local/nginx/html目錄下創建一個 images目錄,并在目錄下放入一張圖片mv.png圖片
location /images {
root /usr/local/nginx/html;
}
訪問圖片的路徑為:
http://192.168.200.133/images/mv.png
(2)如果把root改為alias
location /images {
alias /usr/local/nginx/html;
}
再次訪問上述地址,頁面會出現404的錯誤,查看錯誤日志會發現是因為地址不對,所以驗證了:
root的處理結果是: root路徑+location路徑
/usr/local/nginx/html/images/mv.png
alias的處理結果是:使用alias路徑替換location路徑
/usr/local/nginx/html/images
需要在alias后面路徑改為
location /images {
alias /usr/local/nginx/html/images;
}
(3)如果location路徑是以/結尾,則alias也必須是以/結尾,root沒有要求
將上述配置修改為
location /images/ {
alias /usr/local/nginx/html/images;
}
訪問就會出問題,查看錯誤日志還是路徑不對,所以需要把alias后面加上 /
小結:
root的處理結果是: root路徑+location路徑
alias的處理結果是:使用alias路徑替換location路徑
alias是一個目錄別名的定義,root則是最上層目錄的含義。
如果location路徑是以/結尾,則alias也必須是以/結尾,root沒有要求
index指令
index:設置網站的默認首頁
語法 | index file ...; |
默認值 | index index.html; |
位置 | http、server、location |
index后面可以跟多個設置,如果訪問的時候沒有指定具體訪問的資源,則會依次進行查找,找到第一個為止。
舉例說明:
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
訪問該location的時候,可以通過 http://ip:port/,地址后面如果不添加任何內容,則默認依次訪問index.html和index.htm,找到第一個來進行返回
error_page指令
error_page:設置網站的錯誤頁面
語法 | error_page code ... [=[response]] uri; |
默認值 | — |
位置 | http、server、location...... |
當出現對應的響應code后,如何來處理。
舉例說明:
(1)可以指定具體跳轉的地址
server {
error_page 404 http://www.itcast.cn;
}
(2)可以指定重定向地址
server{
error_page 404 /50x.html;
error_page 500 502 503 504 /50x.html;
location =/50x.html{
root html;
}
}
(3)使用location的@符合完成錯誤信息展示
server{
error_page 404 @jump_to_error;
location @jump_to_error {
default_type text/plain;
return 404 'Not Found Page...';
}
}
可選項=[response]的作用是用來將相應代碼更改為另外一個
server{
error_page 404 =200 /50x.html;
location =/50x.html{
root html;
}
}
這樣的話,當返回404找不到對應的資源的時候,在瀏覽器上可以看到,最終返回的狀態碼是200,這塊需要注意下,編寫error_page后面的內容,404后面需要加空格,200前面不能加空格。
、多站點配置的引入
文件/etc/nginx/nginx.conf 中引入文件夾/etc/nginx/conf.d中的站點配置文件。
引入的代碼為:
include /etc/nginx/conf.d/*.conf;
二、nginx的配置文件
1、查看nginx進程
systemctl status nginx
2、做語法檢測,檢測你修改的配置是不是存在問題
nginx -t
3、查看nginx所用的端口
netstat -anlpt | grep nginx
4、修改nginx的默認端口
vi /etc/nginx/conf.d/default.conf
5、查看進程pid
ps aux | grep nginx
6、pid文件(每個以.pid結尾的文件里面都有一個pid,每個文件對應一個應用)
/var/run
7、查看nginx的pid
/var/run/nginx.pid
8、多站點配置文件放置的位置
文件/etc/nginx/nginx.conf 中引入文件夾/etc/nginx/conf.d中的站點配置文件。
引入的代碼為:
include /etc/nginx/conf.d/*.conf;
9、查看nginx的日志文件
more /var/log/nginx/error.log
10、修改nginx配置文件后的操作
10.1、檢測配置是否正確
nginx -t
10.2、重啟nginx服務
systemctl reload nginx
11、查看端口
netstat -anlpt | grep nginx
三、nginx 文件結構
1、結構如下:
... #全局塊
events { #events塊
...}
http #http塊{
... #http全局塊
server #server塊
{
... #server全局塊
location [PATTERN] #location塊
{
...
}
location [PATTERN]
{
...
}
}
server
{
...
}
... #http全局塊}
2、配置文件結構說明:
1、全局塊:配置影響nginx全局的指令。一般有運行nginx服務器的用戶組,nginx進程pid存放路徑,日志存放路徑,配置文件引入,允許生成worker process數等。
2、events塊(Main配置段,是核心配置段):配置影響nginx服務器或與用戶的網絡連接。有每個進程的最大連接數,選取哪種事件驅動模型處理連接請求,是否允許同時接受多個網路連接,開啟多個網絡連接序列化等。
Events {
#配置事件的
}
3、http塊(標準http配置段,可選http配置段,都在http{}這里配置,每一種都是一個獨立的):可以嵌套多個server,配置代理,緩存,日志定義等絕大多數功能和第三方模塊的配置。如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時時間,單連接請求數等。
4、server塊:配置虛擬主機的相關參數,一個http中可以有多個server。
5、location塊:配置請求的路由,以及各種頁面的處理情況。
注意:每一個配置參數都需要以分號結尾
三、多站點的配置
1、在文件/etc/nginx/conf.d中創建對應的配置文件,文件的名字可以任意取,但為了便于記憶和便于理解可以把文件名配置成相關的域名
2、有多少個站點就配置多少個配置文件
3、配置文件詳細代碼
底層: Windows 11 x64
虛擬化層:VMWare Workstation 17.0.2
虛擬化兼容性:ESXi 6.7
虛擬硬件:
CPU: 4 Core
RAM: 16 GB
HDD: 128 GB SCSI
NetWork: NAT
操作系統版本: CentOS 7.9.2207 Mininal 無更新、無升級內核
無系統更新
無內核升級
關閉 SELinux
配置 NTP 時間同步
安裝 Open VM Tools
美化 CLI
# 虛擬目錄:也叫別名目錄,將任意位置的網站發布到站點根目錄下,用戶通過子目錄方式訪問
# Nginx 定義了兩種 虛擬目錄,分別為 絕對別名 和 相對別名
# 絕對別名目錄:網站數據目錄可以在任意路徑下并發布到默認站點的根目錄下,用戶通過子目錄方式訪問
# 相對別名目錄:網站數據目錄在默認站點根目錄下的多層子目錄下,并發布到默認站點的根目錄下,用戶通過子目錄方式訪問,一般不推薦使用這種方式
# 默認網站 數據目錄
/data/www/
# 創建兩個 網站數據目錄
AAA 站點 /var/www/aaa
BBB 站點 /var/www/bbb
# 設定 網站
AAA站點 /var/www/aaa 訪問路徑為 http://{URL}/a_site
BBB站點 /var/www/bbb 訪問路徑為 http://{URL}/b_site
# 創建 AAA 及 BBB 虛擬目錄
mkdir -p /var/www/aaa && mkdir -p /var/www/bbb
# 創建 虛擬目錄 AAA 主頁文件
echo "Welcom To FourLeaf Studio by A Site <br> Web Data /var/www/aaa" > /var/www/aaa/index.html
# 創建 虛擬目錄 BBB 主頁文件
echo "Welcom To FourLeaf Studio by B Site <br> Web Data /var/www/bbb" > /var/www/bbb/index.html
# 修改 Nginx 默認配置文件
nano /etc/nginx/conf.d/default.conf
# {
# 在 第 10 行,默認網站 配置之后,增加兩段內容
# 絕對別名 虛擬目錄 AAA
location /a_site {
alias /var/www/aaa/;
}
# 絕對別名 虛擬目錄 BBB
location /b_site {
alias /var/www/bbb/;
}
# }
# 重新啟動 Nginx 服務
systemctl restart nginx.service
# 絕對別名 虛擬目錄 訪問地址
http://{URL}/a_site
http://{URL}/b_site
AAA 站點 /var/www/aaa 對應 A Site 地址 虛擬目錄
BBB 站點 /var/www/bbb 對應 B Site 地址 虛擬目錄
# 默認網站 數據目錄
/data/www/
# 創建兩個 網站數據目錄
CCC 站點 /data/www/test/ccc
DDD 站點 /data/www/test/ddd
# 設定 網站
CCC 站點 /data/www/test/ccc 實際訪問路徑為 http://{URL}/test/cc
CCC 站點 /data/www/test/ccc 虛擬訪問路徑為 http://{URL}/ccc
DDD 站點 /data/www/test/ddd 實際訪問路徑為 http://{URL}/test/ddd
DDD 站點 /data/www/test/ddd 虛擬訪問路徑為 http://{URL}/ddd
# 創建 CCC 及 DDD 虛擬目錄
mkdir -p /data/www/test/ccc && mkdir -p /data/www/test/ddd
# 創建 虛擬目錄 CCC 主頁文件
echo "Welcom To FourLeaf Studio by A Site <br> Web Data /data/www/test/ccc" > /data/www/test/ccc/index.html
# 創建 虛擬目錄 DDD 主頁文件
echo "Welcom To FourLeaf Studio by B Site <br> Web Data /data/www/test/ddd" > /data/www/test/ddd/index.html
# 相對別名,一般用于網站根目錄,進行路徑的虛擬,一般不推薦使用
nano /etc/nginx/conf.d/default.conf
# 相對別名,地址設置,僅需要設置到絕對路徑的上一級路徑,并標識完整目錄名稱,不需要定義到完整路徑
# {
# 在 第 10 行,默認網站 配置之后,增加兩段內容
# 相對別名 虛擬目錄 CCC
location /ccc {
root /data/www/test/;
}
# 相對別名 虛擬目錄 DDD
location /ddd {
root /data/www/test/;
}
# }
奇葩的設定方式
# 重新啟動 Nginx 服務
systemctl restart nginx.service
# 實際訪問路徑 地址
http://{Server_IP}/test/ccc
http://{Server_IP}/test/ddd
# 虛擬訪問路徑 地址
http://{Server_IP}/ccc
http://{Server_IP}/ddd
CCC Site 實際訪問路徑及虛擬目錄訪問路徑
DDD Site 實際訪問路徑及虛擬目錄訪問路徑
*請認真填寫需求信息,我們會在24小時內與您取得聯系。