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的值以斜杠结尾报错。
}
}

nginx发布静态资源的更多相关文章

  1. nginx处理静态资源的配置

    修改nginx.conf文件,用于nginx处理静态资源. 主要配置如下(在server配置中加入location配置即可): server { listen 80; server_name 123. ...

  2. linux使用Nginx搭建静态资源服务器

    最近公司需要做一个宣传片播放  视频有点大 好几百M 就想到使用Nginx来代理静态资源,在过程中出现了一些问题,比如端口没开.访问是403等,没有成功,后面慢慢查找问题,才发现大部分博客资料的都不全 ...

  3. nginx 配置静态资源路径(url不同于static path)

    目的         用nginx做静态资源代理可以减少请求对后台服务器的压力,使响应更加迅速. 配置        情景一           url : 127.0.0.1:8000/images ...

  4. Nginx之静态资源WEB服务

    本篇主要记录学习Nginx的静态资源WEB服务的几种常见的功能记录学习 Nginx开发常用的命令 nginx -tc /etc/nginx/nginx.conf vim /etc/nginx/conf ...

  5. nginx缓存静态资源,只需几个配置提升10倍页面加载速度

    nginx缓存静态资源,只需几个配置提升10倍页面加载速度 首先我们看图说话 这是在没有缓存的情况下,这个页面发送了很多静态资源的请求:   1.png 可以看到,静态资源占用了整个页面加载用时的90 ...

  6. nginx/apache静态资源跨域访问问题详解

    1. apache静态资源跨域访问 找到apache配置文件httpd.conf 找到这行 #LoadModule headers_module modules/mod_headers.so把#注释符 ...

  7. nginx配置静态资源:配置绝对路径

    nginx配置静态资源:配置绝对路径 项目都是html格式的文件,我的项目路径:E:\javaservice\nginx-1.15.7\html assets:静态资源 html:站点文件 uploa ...

  8. nginx配置静态资源与动态访问分离【转】

    在前面的博客中<说说 NGINX 的配置及优化>的 2.5 小节里面,提到 location 模块是 nginx 中用的最多的,也是最重要的模块,负载均衡.反向代理.虚拟域名等都与它相关. ...

  9. nginx 作为静态资源web服务

    Nginx作为静态资源web服务 静态资源web服务-CDN场景 Nginx资源存储中心会把静态资源分发给“北京Nginx”,“湖南Nginx”,“山东Nginx”. 然后北京User发送静态资源请求 ...

随机推荐

  1. CF1010D Mars rover

    CF1010D Mars rover 洛谷评测传送门 题目描述 Natasha travels around Mars in the Mars rover. But suddenly it broke ...

  2. pytest--生成HTML报告

    前戏 我们做自动化,测试报告是必不可少的.方便自己查看,也可以供领导查看,一个测试报告就可以说明我们做了哪些事情,pytest-html插件给我们提供了一个很简陋的测试报告,为什么说简陋,因为是真简陋 ...

  3. 今天好像找到C语言延迟输出的原因了

    有时候运行c 第一行printf就像卡住一样.原来是这样<>>>>>>> int a; printf_s("input one number: ...

  4. Excel已损坏,无法打开

    突然之间,很多EXCEL文件打开时报错:"已损坏,无法打开",这些文件共同点是从邮件中下载而来,这些文件可能面临着安全威协,原来是软件设置了受保护的视图,取消即可.

  5. B1013(通过)

    这种方法是采用B1017的那个求素数的算法,并且送一个比较大的数值当作上线(20000),也可以进一步压缩,但是这个数已经够用了,就没有再试了. python方便是方便,但是真的慢 def isPri ...

  6. 工具类docker for k8s

    alpine-tools 安装了常用 工具,curl,telnet, wget 等 apiVersion: extensions/v1beta1 kind: Deployment metadata: ...

  7. python-5-str常用操作

    前言 本节将讲解的是字符串 str 常用的操作方法,与 for 循环. 一.srt 常用操作 1.首个字母大写: # 1.首个字母大写 s = 'xiao long' s1 = s.capitaliz ...

  8. pixijs shader颗粒化显示贴图

    pixijs shader颗粒化显示贴图 const app = new PIXI.Application({ transparent: true }); document.body.appendCh ...

  9. 解决centos ssh连接很慢的问题

    更改配置文件vi /etc/ssh/sshd_config找到UseDNS 将UseDNS前面的#删除,并将YES改为NO,若找不到UseDNS,则手动添加UseDNS,并将其设置成No保存并重启ss ...

  10. 转:对softmax讲解比较清楚的博客

    https://blog.csdn.net/wgj99991111/article/details/83586508