nginx发布静态资源

nginx发布静态资源

名词

server location root alias

参考

Beginner's Guide

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 the default 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、referer和origin的区别

[HTTP趣谈]origin,referer和host区别

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的值以斜杠结尾报错。
    }
}
posted @ 2019-10-29 19:12  没有理由不会呀  阅读(2209)  评论(0编辑  收藏  举报