nginx發布靜態資源
名詞
server
location
root
alias
參考
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
ngx_http_index_module index指令
ngx_http_core_module http指令 location指令 listen指令 root指令 server指令 server_name指令
Nginx之坑:完全理解location中的index,配置網站初始頁
https://blog.csdn.net/qq_32331073/article/details/81945134
配置靜態資源服務
http://www.mozq.com:9001/=> d:/00/00 目錄下的資源
訪問流程分析:
- 請求地址 http://www.mozq.com:9001/static/
- DNS解析請求域名www.mozq.com得到請求主機ip,根據ip地址將請求發到安裝nginx的服務器的指定端口。
- nginx根據端口號,找到對應的server指令,根據指令中的location指令進行處理。
server {
listen 9002;
server_name localhost;
location / {
root d:/00/00/;
# root可以使用右斜杠結尾。
}
}
server {
listen 9003;
server_name localhost;
location / {
root mozq/;
# root的參數可以是相對路徑。這個路徑的當前目錄為nginx安裝目錄。
# 如nginx的安裝目錄為 D:\chengxu\nginx\nginx-1.16.1
# 則 mozq/ 表示的絕對路徑為 D:\chengxu\nginx\nginx-1.16.1\mozq\
}
}
D:\chengxu\nginx\nginx-1.16.1>tree
卷 DATA 的文件夾 PATH 列表
卷序列號為 14DC-019C
D:.
├─conf
├─contrib
│ ├─unicode2nginx
│ └─vim
│ ├─ftdetect
│ ├─ftplugin
│ ├─indent
│ └─syntax
├─docs
├─html
├─logs
├─mozq
# mozq為安裝在nginx目錄中的文件夾。
└─temp
├─client_body_temp
├─fastcgi_temp
├─proxy_temp
├─scgi_temp
└─uwsgi_temp
root指令和alias指令的區別
http://nginx.org/en/docs/http/ngx_http_core_module.html#alias
http://nginx.org/en/docs/http/ngx_http_core_module.html#root
-
root指令參數后直接拼接uri
-
alias會將uri中匹配location中的部分擦除后拼接到參數后。
alias配置
請求
http://localhost:9002/mozq/static/shouce.jpg
配置1(正確)
server {
listen 9002;
server_name localhost;
location /mozq/static {
alias d:/00/00/;
# uri /mozq/static/shouce.jpg匹配location,因為是alias去除匹配location部分,剩下的是/shouce.jpg
# d:/00/00/ + /shouce.jpg = d:/00/00//shouce.jpg 然后規范化一下不報錯,正常。
}
}
配置2(錯誤)
server {
listen 9002;
server_name localhost;
location /mozq/static/ {
alias d:/00/00;
# uri /mozq/static/shouce.jpg匹配location,因為是alias去除匹配location部分,剩下的是shouce.jpg
# d:/00/00 + shouce.jpg = d:/00/00shouce.jpg 然后規范化一下不報錯,但是這不是我們想要的結果。
}
}
root配置
請求
http://localhost:9002/mozq/static/shouce.jpg
server {
listen 9002;
server_name localhost;
location /mozq/static/ {
root d:/00/00;
# uri /mozq/static/shouce.jpg匹配location
# d:/00/00 + /mozq/static/shouce.jpg = d:/00/00/mozq/static/shouce.jpg
}
}
線上配置
location /smartcard_web {
alias /usr/local/javaHome/smartcard/smartcard_web;
}
另一種方式:
location /smartcard_web {
root /usr/local/javaHome/smartcard/;
}
請求應該被哪個server塊處理
參考
http://nginx.org/en/docs/http/request_processing.html
listen
server_name
default_server
- 根據請求的端口找到server,一個端口可以配置多個server,所以找到的是多個。
- 根據server_name參數的值和請求中的
Host
請求頭進行匹配 - 匹配到了則使用這個,如果沒有匹配到則使用這個端口默認的server
- 如果請求中沒有
Host
請求頭,則也匹配默認的server - 默認的server是配置文件中這個端口對應的第一個server,也可以用default_server顯示指定端口的默認server。
- server_name 的默認值是 "",表示其處理沒有攜帶
Host
請求頭的請求。
# server_name參數的作用
server {
listen 91;
server_name video.mozq.com;
location /mozq/image {
alias d:/00/00/mozq2;
}
}
server {
listen 91 default_server;
server_name "" image.mozq.com;
location /mozq/image {
alias d:/00/00/mozq;
}
}
處理過程
Name-based virtual servers
nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port *:80:
server { listen 80; server_name example.org www.example.org; ... } server { listen 80; server_name example.net www.example.net; ... } server { listen 80; server_name example.com www.example.com; ... }
In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. In the configuration above, the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server
parameter in the listen directive:
server { listen 80 default_server; server_name example.net www.example.net; ... }
The
default_server
parameter has been available since version 0.8.21. In earlier versions thedefault
parameter should be used instead.
Note that the default server is a property of the listen port and not of the server name. More about this later.
How to prevent processing requests with undefined server names
If requests without the “Host” header field should not be allowed, a server that just drops the requests can be defined:
server { listen 80; server_name ""; return 444; }
Here, the server name is set to an empty string that will match requests without the “Host” header field, and a special nginx’s non-standard code 444 is returned that closes the connection.
Since version 0.8.48, this is the default setting for the server name, so the
server_name ""
can be omitted. In earlier versions, the machine’s hostname was used as a default server name.
請求頭 Host Origin Referer
參考
host比較容易理解,來看下MDN網站給的介紹:
Host 請求頭指明了服務器的域名(對於虛擬主機來說),以及(可選的)服務器監聽的TCP端口號。 如果沒有給定端口號,會自動使用被請求服務的默認端口(比如請求一個HTTP的URL會自動使用80端口)。 HTTP/1.1 的所有請求報文中必須包含一個Host頭字段。如果一個 HTTP/1.1 請求缺少 Host 頭字段或者設置了超過一個的 Host 頭字段,一個400(Bad Request)狀態碼會被返回。
從上面的文字中可以總結出如下信息:
1、host的值為客戶端請求的服務器的域名(或者ip)和端口
2、http/1.1中必須包含host請求頭,且只能設置一個;
那么host主要用在什么地方呢?
host用的最多的場景是:單台服務器設置多個虛擬主機時。
舉個簡單的例子: 我在IP地址為127.0.0.1的服務器上,通過apache配置了兩個虛擬主機:a.com,b.com,這兩個域名通過DNS解析都會指向127.0.0.1,我在瀏覽器中訪問a.com的網站時,DNS將域名轉化為IP地址,此時可以通過客戶端請求頭的host信息判斷訪問的是服務器上對應的虛擬主機。
Request URL: http://localhost/xc/video/5/f/5fbb79a2016c0eb609ecd0cd3dc48016/hls/5fbb79a2016c0eb609ecd0cd3dc48016_00000.ts
Host: localhost
Pragma: no-cache
Referer: http://localhost/xc/image/front-video/demo.html
Request URL: http://127.0.0.1/xc/video/5/f/5fbb79a2016c0eb609ecd0cd3dc48016/hls/5fbb79a2016c0eb609ecd0cd3dc48016_00000.ts
Request Method: GET
Host: 127.0.0.1
Origin: http://localhost
Pragma: no-cache
Referer: http://localhost/xc/image/front-video/demo.html
Request URL: http://image.mozq.com/xc/video/5/f/5fbb79a2016c0eb609ecd0cd3dc48016/hls/5fbb79a2016c0eb609ecd0cd3dc48016_00000.ts
Host: image.mozq.com
Origin: http://localhost
Pragma: no-cache
Referer: http://localhost/xc/video/front-video/demo.html
1. Host
描述請求將被發送的目的地,包括,且僅僅包括域名和端口號。
在任何類型請求中,request都會包含此header信息。
2. Origin
用來說明請求從哪里發起的,包括,且僅僅包括協議和域名。
這個參數一般只存在於CORS跨域請求中,可以看到response有對應的header:Access-Control-Allow-Origin。
3. Referer
告知服務器請求的原始資源的URI,其用於所有類型的請求,並且包括:協議+域名+查詢參數(注意,不包含錨點信息)。
因為原始的URI中的查詢參數可能包含ID或密碼等敏感信息,如果寫入referer,則可能導致信息泄露。
步驟
創建靜態資源
為 conf/nginx.conf
http模塊中新增server模塊
靜態資源結構
E:\mozq\00store\frxx
├─frxx
│ bug.png
│ weixin.png
server模塊配置
server{
listen 9001;
server_name localhost;
location / {
root E:\mozq\00store\frxx; # root參數不能以斜杠結尾不然報錯。
}
}
# 示例1 成功
http://localhost:9001/weixin.png
# 示例2 失敗
請求: http://localhost:9001
響應:
Request URL: http://localhost:9001/
Request Method: GET
Status Code: 403 Forbidden
# 示例3 失敗
請求: http://localhost:9001/weixin.pn
響應:
Request URL: http://localhost:9001/weixin.pn
Request Method: GET
Status Code: 404 Not Found
# 示例4 失敗
請求: http://localhost:9002/
響應: 無法訪問此網站。因為9002端口根本沒有服務提供者。
server {
listen 9001;
server_name localhost;
location /fr {
# root和alias都不能加斜杠,不然報錯。上面的/fr不能寫成/fr/。
#root D:\00\frxx; # /fr/zhou.html 訪問 D:\00\frxx\fr\zhou.html
alias D:\00\frxx; # /fr/zhou.html 訪問 D:\00\frxx\zhou.html
#index chang.html;
}
}
location塊
# location塊文檔
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
# nginx默認配置
location / {
root html; # 指定資源為nginx主目錄下的html目錄。
index index.html index.htm; # 指定默認首頁列表
}
bugs
E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:\mozq\chengxu\nginx\nginx-1.16.1/conf/nginx.conf:40
錯誤代碼:沒有以分號結尾
location / {
root E:\mozq\00store\frxx
}
正確代碼:以分號結尾
location / {
root E:\mozq\00store\frxx;
}
E:\mozq\00store>nginx -s reload
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2019/10/29 09:12:42 [notice] 9640#17752: using inherited sockets from "E:\mozq\chengxu\nginx\nginx-1.16.1"
2019/10/29 09:12:42 [emerg] 9640#17752: invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring the rest of the variable
2019/10/29 09:12:42 [emerg] 9640#17752: invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring
2019/10/29 09:12:42 [emerg] 9640#17752: CreateFile() "E:\mozq\00store/conf/nginx.conf" failed (3: The system cannot find the path specified)
E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring the rest of the variable
nginx: [emerg] invalid socket number "E:\mozq\chengxu\nginx\nginx-1.16.1" in NGINX environment variable, ignoring
原因:
為nginx設置環境變量為NGINX和其源碼中沖突了。
將環境變量名改為NGINX_HOME
C:\Users\1>nginx -s reload -c E:\mozq\chengxu\nginx\nginx-1.16.1\conf\nginx.conf
nginx: [alert] could not open error log file: CreateFile() "logs/error.log" failed (3: The system cannot find the path specified)
2019/10/29 13:44:10 [notice] 11572#8380: signal process started
2019/10/29 13:44:10 [error] 11572#8380: CreateFile() "C:\Users\1/logs/nginx.pid" failed (3: The system cannot find the path specified)
原因:
指定了配置文件,找不到其他文件。使用-p參數指定nginx目錄
解決:使用 -p 參數指定 nginx 主目錄。
C:\Users\1>nginx -s reload -p E:\mozq\chengxu\nginx\nginx-1.16.1\
E:\mozq\chengxu\nginx\nginx-1.16.1>nginx -s reload
nginx: [emerg] unexpected "}" in E:\mozq\chengxu\nginx\nginx-1.16.1/conf/nginx.conf:40
錯誤代碼:root的值以斜杠結尾報錯。
server{
listen 9001;
server_name localhost;
location / {
root E:\mozq\00store\frxx\; # root的值以斜杠結尾報錯。
}
}