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.
步骤
创建静态资源
为 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的值以斜杠结尾报错。 } }
来源:https://www.cnblogs.com/mozq/p/11760874.html