经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
后端必备 Nginx 配置
来源:cnblogs  作者:光、夜雨微凉  时间:2019/9/17 10:41:56  对本文有异议

后端必备 Nginx 配置

概要

  • 防盗链
  • 根据文件类型设置过期时间
  • 静态资源访问
  • 日志配置
    • 日志字段说明
    • access_log 访问日志
    • error_log 日志
    • 日志切割
  • 反向代理
  • 禁止指定user_agent
  • nginx访问控制
  • 负载均衡

 

防盗链

 

  1. location ~* \.(gif|jpg|png)$ {
  2. # 只允许 192.168.0.1 请求资源
  3. valid_referers none blocked 192.168.0.1;
  4. if ($invalid_referer) {
  5. rewrite ^/ http://$host/logo.png;
  6. }
  7. }

 

根据文件类型设置过期时间

  

  1. location ~.*\.css$ {
  2. expires 1d;
  3. break;
  4. }
  5. location ~.*\.js$ {
  6. expires 1d;
  7. break;
  8. }
  9. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
  10. access_log off;
  11. expires 15d; #保存15天
  12. break;
  13. }
  14. # curl -x127.0.0.1:80 http://www.test.com/static/image/common/logo.png -I #测试图片的max-age

     

静态资源访问

  1. http {
  2. # 这个将为打开文件指定缓存,默认是没有启用的,max 指定缓存数量,
  3. # 建议和打开文件数一致,inactive 是指经过多长时间文件没被请求后删除缓存。
  4. open_file_cache max=204800 inactive=20s;
  5. # open_file_cache 指令中的inactive 参数时间内文件的最少使用次数,
  6. # 如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个
  7. # 文件在inactive 时间内一次没被使用,它将被移除。
  8. open_file_cache_min_uses 1;
  9. # 这个是指多长时间检查一次缓存的有效信息
  10. open_file_cache_valid 30s;
  11. # 默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不
  12. # 少带宽,但是会增加服务器CPU的开销哦,Nginx默认只对text/html进行压缩 ,
  13. # 如果要对html之外的内容进行压缩传输,我们需要手动来设置。
  14. gzip on;
  15. gzip_min_length 1k;
  16. gzip_buffers 4 16k;
  17. gzip_http_version 1.0;
  18. gzip_comp_level 2;
  19. gzip_types text/plain application/x-javascript text/css application/xml;
  20. server {
  21. listen 80;
  22. server_name www.test.com;
  23. charset utf-8;
  24. root /data/www.test.com;
  25. index index.html index.htm;
  26. }
  27. }

 

日志配置

日志字段说明

 

字段说明 
remote_addr 和 http_x_forwarded_for 客户端 IP 地址
remote_user 客户端用户名称
request 请求的 URI 和 HTTP 协议
status 请求状态
body_bytes_sent 返回给客户端的字节数,不包括响应头的大小
bytes_sent 返回给客户端总字节数
connection 连接的序列号
connection_requests 当前同一个 TCP 连接的的请求数量
msec 日志写入时间。单位为秒,精度是毫秒
pipe 如果请求是通过HTTP流水线(pipelined)发送,pipe值为“p”,否则为“.”
http_referer 记录从哪个页面链接访问过来的
http_user_agent 记录客户端浏览器相关信息
request_length 请求的长度(包括请求行,请求头和请求正文)
time_iso8601 ISO8601标准格式下的本地时间
time_local 记录访问时间与时区

 

access_log 访问日志

  1. http {
  2. log_format access '$remote_addr - $remote_user [$time_local] $host "$request" '
  3. '$status $body_bytes_sent "$http_referer" '
  4. '"$http_user_agent" "$http_x_forwarded_for" "$clientip"';
  5. access_log /srv/log/nginx/talk-fun.access.log access;
  6. }复制代码

 

error_log 日志

  1. error_log /srv/log/nginx/nginx_error.log error;
  2. # error_log /dev/null; # 真正的关闭错误日志
  3. http {
  4. # ...
  5. }

 

日志切割

 

 # 和apache不同的是,nginx没有apache一样的工具做切割,需要编写脚本实现。# 在/usr/local/sbin下写脚本

  1. #!/bin/bash
  2. dd=$(date -d '-1 day' +%F)[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log
  3. mv /tmp/nginx_access.log /tmp/nginx_log/$dd.log
  4. /etc/init.d/nginx reload > /dev/null

反向代理

  1. http {
  2. include mime.types;
  3. server_tokens off;
  4. ## 配置反向代理的参数
  5. server {
  6. listen 8080;
  7. ## 1. 用户访问 http://ip:port,则反向代理到 https://github.com
  8. location / {
  9. proxy_pass https://github.com;
  10. proxy_redirect off;
  11. proxy_set_header Host $host;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. }
  15. ## 2.用户访问 http://ip:port/README.md,则反向代理到
  16. ## https://github.com/zibinli/blog/blob/master/README.md
  17. location /README.md {
  18. proxy_set_header X-Real-IP $remote_addr;
  19. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  20. proxy_pass https://github.com/zibinli/blog/blob/master/README.md;
  21. }
  22. }
  23. }

 

禁止指定user_agent

 

  1. #虚拟主机的配置文件里加入:
  2. if ($http_user_agent ~* 'baidu|360|sohu') #禁止useragent为baidu、360和sohu,~*表示不区分大小写匹配
  3. {
  4. return 403;
  5. }
  6. location / location ~ / 优先级是不一样的。
  7. 结合这个文章研究一下吧 http://blog.itpub.net/27181165/viewspace-777202/
  8. curl -A "baidu" -x127.0.0.1:80 www.test.com/forum.php -I 该命令指定百度为user_agent,返回403

  

nginx访问控制

 

  1. # 可以设置一些配置禁止一些ip的访问
  2. deny 127.0.0.1; #全局定义限制,location里的是局部定义的。如果两者冲突,以location这种精确地优先,
  3. location ~ .*admin\.php$ {
  4. #auth_basic "cct auth";
  5. #auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
  6. allow 127.0.0.1; 只允许127.0.0.1的访问,其他均拒绝
  7. deny all;
  8. include fastcgi_params;
  9. fastcgi_pass unix:/tmp/www.sock;
  10. fastcgi_index index.php;
  11. fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
  12. }

 

负载均衡

 
  1. http {
  2. upstream test.net {
  3. ip_hash;
  4. server 192.168.10.13:80;
  5. server 192.168.10.14:80 down;
  6. server 192.168.10.15:8009 max_fails=3 fail_timeout=20s;
  7. server 192.168.10.16:8080;
  8. }
  9. server {
  10. location / {
  11. proxy_pass http://test.net;
  12. }
  13. }
  14. }

原文链接:http://www.cnblogs.com/weilingfeng/p/11531467.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号