#sendfile配置规则
Syntax: sendfile on | off;
Default:
sendfile off;
Context: http, server, location, if in location
--with-file-aio
异步文件读取tcp_nopush
模块官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nopush#tcp_nopush配置规则
Syntax: tcp_nopush on | off;
Default:
tcp_nopush off;
Context: http, server, location
sendfile
开启的情况下,提高网络包的传输效率tcp_nodelay
模块官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#tcp_nodelay#tcp_nodelay配置规则
Syntax: tcp_nodelay on | off;
Default:
tcp_nodelay on;
Context: http, server, location
ngx_http_gzip_module
模块文档地址:http://nginx.org/en/docs/http/ngx_http_gzip_module.html#gzip配置规则
Syntax: gzip on | off;
Default:
gzip off;
Context: http, server, location, if in location
#gzip压缩等级
Syntax: gzip_comp_level level;
Default:
gzip_comp_level 1;
Context: http, server, location
#http版本
Syntax: gzip_http_version 1.0 | 1.1;
Default:
gzip_http_version 1.1;
Context: http, server, location
ngx_http_gzip_static_module
模块文档:http://nginx.org/en/docs/http/ngx_http_gzip_static_module.htmlngx_http_gunzip_module
模块文档:http://nginx.org/en/docs/http/ngx_http_gunzip_module.html#gzip_static配置规则
Syntax: gzip_static on | off | always;
Default:
gzip_static off;
Context: http, server, location
listen 80;
server_name localhost;
sendfile on;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location ~ .*\.(jpg|gif|png)$ {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 2;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/images;
}
location ~ .*\.(txt|xml)$ {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 1;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/
# javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/doc;
}
location ~ ^/download {
#gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
listen 80;
server_name localhost;
sendfile on;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location ~ .*\.(jpg|gif|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/images;
}
location ~ .*\.(txt|xml)$ {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 1;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/
# javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/doc;
}
location ~ ^/download {
#gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
location ~ .*\.(txt|xml)$ {
#gzip on;
#gzip_http_version 1.1;
#gzip_comp_level 1;
#gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/
# javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/doc;
}
location ~ ^/download {
#gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
#如果出现配置正确访问出现404则文件用户需要修改为nginx
chown nginx [文件名]
location ~ .*\.(txt|xml)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 1;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
root /opt/app/code/doc;
}
location ~ ^/download {
gzip_static on;
tcp_nopush on;
root /opt/app/code;
}
如果
gzip_static off
的话直接访问http://192.168.0.105/download/test.img
是404并且不会下载.
HTTP协议定义的缓存机制(如:Expires Cache-control等)
Cache-Control, Expires
头ngx_http_headers_module
官方文档:http://nginx.org/en/docs/http/ngx_http_headers_module.html#配置规则
Syntax: expires [modified] time;
expires epoch | max | off;
Default:
expires off;
Context: http, server, location, if in location
#配置方式
location ~ .*\.(html|htm)$ {
#expires 2m;
root /opt/app/code;
}
location ~ .*\.(html|htm)$ {
expires 2m;
root /opt/app/code;
}
跨站访问不安全,容易出现CSRF攻击!
ngx_http_headers_module
官方文档:http://nginx.org/en/docs/http/ngx_http_headers_module.html#配置规则
Syntax: add_header name value [always];
Default: —
Context: http, server, location, if in location
#Access-Control-Allow-Origin
#跨站请求代码
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://192.168.0.105/1.html",
success: function(data) {
alert("sucess!!!") ;
},
error: function(){
alert("fail!!!,请刷新再试!");
}
});
});
</script>
<body>
<h1>测试跨域访问</h1>
</body>
</html>
#配置方法
location ~ .*\.(html|htm)$ {
add_header Access-Control-Allow-Origin http://192.168.0.105;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
root /opt/app/code;
}
目的:防止资源被盗用. 防盗链设置思路:区别那些请求是非正常用户的请求.
ngx_http_referer_module
模块官方文档:http://nginx.org/en/docs/http/ngx_http_referer_module.html#设置规则
Syntax: valid_referers none | blocked | server_names | string ...;
Default: —
Context: server, location
#配置方法
location ~ .*\.(jpg|gif|png)$ {
gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
valid_referers none blocked 192.168.0.105;
if ($invalid_referer) { #如果$invalid_referer非0 则返回403
return 403;
}
root /opt/app/code/images;
}
referer
防盗功能有限,
版权属于:龙之介大人
本文链接:https://cloud.tencent.com/developer/article/1619676
本站所有原创文章采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。 您可以自由的转载和修改,但请务必注明文章来源和作者署名并说明文章非原创且不可用于商业目的。