[关闭]
@dooy 2022-12-19T08:09:31.000000Z 字数 2934 阅读 249

Nginx 压缩 缓存 安全 OpenResty 跨域

Nginx

压缩

很多时候我们以为 gzip on; 就以为都会压缩了,其实他只压缩了html 或者php输出是html;而对于css js 图片等都没有压缩。 如何配置 css js 图片都压缩了

  1. gzip on;
  2. gzip_min_length 1k;
  3. gzip_buffers 4 16k;
  4. gzip_comp_level 2;
  5. gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/javascript; #根据header来压缩
  6. gzip_vary on;
  7. gzip_disable "MSIE [1-6]\."; #ie 不压缩

缓存

在 http{} 中加入

  1. proxy_temp_path pigai_cache 1 2;
  2. proxy_cache_path pigai_cache/pigai levels=1:2 keys_zone=pigai:100m inactive=1d max_size=10g;

在你加的server{} 中加入

  1. location ~ .*\.(js|css|gif|jpg|jpeg|png|bmp|swf|flv)$
  2. {
  3. proxy_pass http://pigai;
  4. expires 6h; #设置浏览器过期时间
  5. proxy_cache pigai;#使用pigai这个keys_zone
  6. proxy_cache_key $host$uri$is_args$args;
  7. proxy_cache_valid 200 302 1h;#200302状态码保存1小时
  8. proxy_cache_valid 301 1d;#301状态码保存一天
  9. proxy_cache_valid any 1m;#其它的保存一分钟
  10. add_header X-Cache $upstream_cache_status; #测试的时候看看在hit否?
  11. }

限制速度

只对链接中的链接数做统计
在 http{} 中加入

  1. limit_conn_zone $binary_remote_addr zone=limit:10m; #这边使用ip的二进制 如果是内部ip通过层层代理 可以使用 $proxy_add_x_forwarded_for 来计算
  2. limit_conn_log_level info;
  3. limit_conn_status 403;

在你加的server{} 中加入

  1. location ~ /(zt|about|corpus|dm) {
  2. limit_conn limit 3;
  3. }

安全

pigai_anquan.conf

  1. ## xss 安全
  2. if ( $args ~* "set([^&=]+)var" ) {
  3. return 403;
  4. }
  5. if ( $args ~* "set(.+)echo" ) {
  6. return 403;
  7. }
  8. if ( $args ~* "this\.src" ) {
  9. return 403;
  10. }
  11. if ( $args ~* "(iframe%3E|%2fiframe)" ) {
  12. return 403;
  13. }
  14. if ( $args ~* "(script%3E|%2fscript)" ) {
  15. return 403;
  16. }
  17. ## 系统
  18. if ( $args ~* "(etc\/passwd)" ) {
  19. return 403;
  20. }
  21. #下面的范文比较大 请谨慎使用
  22. if ( $args ~* "%3C" ) {
  23. return 403;
  24. }
  25. if ( $args ~* "%3E" ) {
  26. return 403;
  27. }
  28. if ( $args ~* "\(*+\)" ) {
  29. return 403;
  30. }

使用OpenResty在 proxy_pass 下一行 或者 有php出现的地方加入

  1. header_filter_by_lua_block {
  2. ngx.header["X-Powered-By"] = nil
  3. }

跨域

  1. map $http_origin $corsHost {
  2. default 0;
  3. "~http://gj.pigai.org" http://gj.pigai.org;
  4. "~http://gj2.pigai.org:8081" http://gj2.pigai.org:8081;
  5. "~http://gj2.pigai.org" http://gj2.pigai.org;
  6. }
  7. //上面的放在 NGINX.conf 下 下面的放在 location 下
  8. if ($request_method = 'OPTIONS') {
  9. add_header 'Access-Control-Allow-Origin' $corsHost;
  10. add_header 'Access-Control-Allow-Credentials' 'true';
  11. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  12. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  13. add_header 'Access-Control-Max-Age' 1728000;
  14. add_header 'Content-Type' 'text/plain charset=UTF-8';
  15. add_header 'Content-Length' 0;
  16. return 204;
  17. }
  18. if ($request_method = 'POST') {
  19. add_header 'Access-Control-Allow-Origin' $corsHost;
  20. add_header 'Access-Control-Allow-Credentials' 'true';
  21. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  22. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  23. }
  24. if ($request_method = 'GET') {
  25. add_header 'Access-Control-Allow-Origin' $corsHost;
  26. add_header 'Access-Control-Allow-Credentials' 'true';
  27. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  28. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
  29. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注