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. CF1225C p-binary

    CF1225C p-binary 洛谷评测传送门 题目描述 Vasya will fancy any number as long as it is an integer power of two. ...

  2. CentOS7 部署 Django 项目

    1. 更新系统软件包 yum update -y 2. 安装软件管理包和可能使用的依赖 yum -y groupinstall "Development tools" yum in ...

  3. CSP-J&S2019前颓废记

    说了是颓废记,就是颓废记,因为真的很颓废...... 2018年12月 我看懂了<啊哈算法>(仅仅是看懂,并没有完全学会,只看得懂,却不会敲) 插曲:八上期末考试 我们老师阻挠我继续学OI ...

  4. Pytorch数据类型转换

    Pytorch数据类型转换 载入模块生成数据 import torch import numpy as np a_numpy = np.array([1,2,3]) Numpy转换为Tensor a_ ...

  5. pywinauto教程2

    一.环境安装 1.命令行安装方法 pip install pywinauto==0.6.7 2.手动安装方法 安装包下载链接:pyWin32: python调用windows api的库https:/ ...

  6. 海边拾贝-E-第三方专栏文章

    收录一些优秀的专栏文章,或者解决某类知识点的文章: Linux高性能服务器编程:https://blog.csdn.net/gatieme/column/info/high-per-server ht ...

  7. Spring源码系列 — 构造和初始化上下文

    探索spring源码实现,精华的设计模式,各种jdk提供的陌生api,还有那么点黑科技都是一直以来想做的一件事!但是读源码是一件非常痛苦的事情,需要有很大的耐心和扎实的基础. 在曾经读两次失败的基础上 ...

  8. 转 经典分类网络Googlenet

    转自https://my.oschina.net/u/876354/blog/1637819 2014年,GoogLeNet和VGG是当年ImageNet挑战赛(ILSVRC14)的双雄,GoogLe ...

  9. kali渗透综合靶机(六)--FristiLeaks靶机

    kali渗透综合靶机(六)--FristiLeaks靶机 靶机地址下载:https://download.vulnhub.com/fristileaks/FristiLeaks_1.3.ova 一.主 ...

  10. 使用SolrJ客户端管理SolrCloud(Solr集群)

    1.使用SolrJ客户端管理SolrCloud(Solr集群). package com.taotao.search.service; import java.io.IOException; impo ...