nginx-add_header指令

介绍

前端子系统由于业务形态,会有各种相同根域名的子系统相互调用,这时候就需要相应的子系统在nginx上支持跨域配置。

业务使用的样例

add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Headers' 'accept,os,accesstoken,content-Type,X-Requested-With,Authorization,apptype,appkey,devid,token,uid,versioncode,versionname,mfg,x-request-id,x-request-uid';
add_header 'Access-Control-Max-Age' '2592000';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, OPTIONS, POST, DELETE';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'X-Content-Type-Options' 'nosniff';
add_header 'X-XSS-Protection' '1; mode=block';
add_header 'X-Frame-Options' 'SAMEORIGIN';

语法

Syntax: add_header name value [always];
Default:    —
Context:    http, server, location, if in location
Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.

(如果响应代码等于 200、201 (1.3.10)、204、206、301、302、303、304、307 (1.1.16、1.0.13) 或 308 (1.13),则将指定字段添加到响应标头.0)。参数值可以包含变量。)
==> add_header 指令用于添加返回头字段,当且仅当状态码为图中列出的那些时有效。

There could be several add_header directives. These directives are inherited from the previous configuration level if and only if there are no add_header directives defined on the current level.

(可能有几个add_header指令。add_header当且仅当在当前级别上 没有定义指令时,这些指令才从先前的配置级别继承。)
--> 每一层都可以从上层继承 add_header,但是如果当前层添加了add_header,则不能继承。

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.

(如果always指定了参数(1.7.5),则无论响应代码如何,都会添加标头字段。)
==> 如果提供了第三个参数 always,那么无论状态码是多少,添加标头字段都会带上

示例

add_header 指令用于添加返回头字段,当且仅当状态码为(ngx_http_headers_module)列出的那些时有效。

有效状态码: 200、201 (1.3.10)、204、206、301、302、303、304、307 (1.1.16、1.0.13) 或 308

        location / {
        add_header yanzheng ok;
            root   html;
            index  index.html index.htm;
        }

nginx-add_header指令

设置一个非有效状态码,测试 add_header发现不会返回add_header自定义返回头字段

        location / {
        add_header yanzheng ok;
        return 400;
            root   html;
            index  index.html index.htm;
        }

nginx-add_header指令

不根据有效状态码返回,强制返回add_header自定义返回头字段
如果提供了第三个参数 always,那么无论状态码是多少,添加标头字段都会带上

        location / {
            add_header yanzheng ok always;
            return 400;
            root   html;
            index  index.html index.htm;
        }

nginx-add_header指令

每一层都可以从上层继承 add_header,但是如果当前层添加了add_header,则不能从上层继承。

Context: http, server, location, if in location

http 模块
server 模块
location 模块
location中的if模块

将静态资源(css、js)设置为强缓存,将html文件设置为协商缓存,cache-control:no-cache,避免html也被缓存,导致用户不能及时更新最新的内容

cat /usr/share/nginx/html/1.js
test

cat /etc/nginx/nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        location / {
            add_header yanzheng ok always;
            #return 400;
            root   html;
            index  index.html index.htm;

          if ($request_filename ~ .*\.(html|htm)$)
            {
                    add_header cache-control no-cache;
            }

        }

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

对于index.html文件,cache-controll生效了,但是自定义header yanzheng 不见

nginx-add_header指令

对于1.js文件,自定义header yanzheng 保留了

nginx-add_header指令

原因: 当判断 html 文件时,使用到了 if 模块,并且在 if 模块中使用了 add_header,导致没有继承到最外层的 add_header test yanzheng
解决: 把外层的add_header复制一份。。[顺序: http, server, location, if in location . 在里面的要复制外面的add_header]
注意: 当add_header指令用的很多时,可以抽离成.conf文件,通过include进行引入。

cat /usr/share/nginx/html/1.js
test

cat /etc/nginx/nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        location / {
            add_header yanzheng ok always;
            #return 400;
            root   html;
            index  index.html index.htm;

          if ($request_filename ~ .*\.(html|htm)$)
            {
                    add_header cache-control no-cache;
                    add_header yanzheng ok always;
            }

        }

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

把外层的add_header复制一份后,对于index.html文件,cache-controll生效了,自定义header yanzheng 存在; 对于1.js文件,自定义header yanzheng 也保留了

nginx-add_header指令
Syntax: try_files file ... uri;
try_files file ... =code;
Default:    —
Context:    server, location

详细参考 http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
try_files指令,作用是按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),
如果所有的文件或文件夹都找不到,会进行内部重定向到最后一个参数
cat /usr/share/nginx/html/1.js
test

cat /etc/nginx/nginx.conf
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        location / {
            add_header html ok always;
            root   html;
            index  index.html index.htm;
        }

         location /js {
            add_header js ok;
            try_files $uri /;
        }

        # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

try_files都是最后的url位置生效.

nginx-add_header指令

nginx 返回状态码

200 (成功) 服务器已成功处理了请求。 通常,这表示服务器提供了请求的网页。
201 (已创建) 请求成功并且服务器创建了新的资源。
202 (已接受) 服务器已接受请求,但尚未处理。
203 (非授权信息) 服务器已成功处理了请求,但返回的信息可能来自另一来源。
204 (无内容) 服务器成功处理了请求,但没有返回任何内容。
205 (重置内容) 服务器成功处理了请求,但没有返回任何内容。
206 (部分内容) 服务器成功处理了部分 GET 请求。

300 (多种选择) 针对请求,服务器可执行多种操作。 服务器可根据请求者 (user agent) 选择一项操作,或提供操作列表供请求者选择。
301 (永久移动) 请求的网页已永久移动到新位置。 服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自动将请求者转到新位置。
302 (临时移动) 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。
303 (查看其他位置) 请求者应当对不同的位置使用单独的 GET 请求来检索响应时,服务器返回此代码。
304 (未修改) 自从上次请求后,请求的网页未修改过。 服务器返回此响应时,不会返回网页内容。
305 (使用代理) 请求者只能使用代理访问请求的网页。 如果服务器返回此响应,还表示请求者应使用代理。
307 (临时重定向) 服务器目前从不同位置的网页响应请求,但请求者应继续使用原有位置来进行以后的请求。

400 (错误请求) 服务器不理解请求的语法。
401 (未授权) 请求要求身份验证。 对于需要登录的网页,服务器可能返回此响应。
403 (禁止) 服务器拒绝请求。
404 (未找到) 服务器找不到请求的网页。
405 (方法禁用) 禁用请求中指定的方法。
406 (不接受) 无法使用请求的内容特性响应请求的网页。
407 (需要代理授权) 此状态代码与 401(未授权)类似,但指定请求者应当授权使用代理。
408 (请求超时) 服务器等候请求时发生超时。
409 (冲突) 服务器在完成请求时发生冲突。 服务器必须在响应中包含有关冲突的信息。
410 (已删除) 如果请求的资源已永久删除,服务器就会返回此响应。
411 (需要有效长度) 服务器不接受不含有效内容长度标头字段的请求。
412 (未满足前提条件) 服务器未满足请求者在请求中设置的其中一个前提条件。
413 (请求实体过大) 服务器无法处理请求,因为请求实体过大,超出服务器的处理能力。
414 (请求的 URI 过长) 请求的 URI(通常为网址)过长,服务器无法处理。
415 (不支持的媒体类型) 请求的格式不受请求页面的支持。
416 (请求范围不符合要求) 如果页面无法提供请求的范围,则服务器会返回此状态代码。
417 (未满足期望值) 服务器未满足"期望"请求标头字段的要求。

500 (服务器内部错误) 服务器遇到错误,无法完成请求。
501 (尚未实施) 服务器不具备完成请求的功能。 例如,服务器无法识别请求方法时可能会返回此代码。
502 (错误网关) 服务器作为网关或代理,从上游服务器收到无效响应。
503 (服务不可用) 服务器目前无法使用(由于超载或停机维护)。 通常,这只是暂时状态。
504 (网关超时) 服务器作为网关或代理,但是没有及时从上游服务器收到请求。
505 (HTTP 版本不受支持) 服务器不支持请求中所用的 HTTP 协议版本。

参考

nginx-github
https://github.com/nginx/

nginx-documentation-[ngx_http_headers_module]
http://nginx.org/en/docs/http/ngx_http_headers_module.html

nginx-documentation-[ngx_http_core_module]
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

Original: https://www.cnblogs.com/linuxshare/p/16499037.html
Author: 爱折腾的大臭臭
Title: nginx-add_header指令

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/573864/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球