关于nginx:编程技巧│超实用-nginx-中常见的配置合集

🎈 封禁 IP

  • 通过 deny 能够封禁指定 IP
http {
    # ....
    # 封禁IP
    deny 192.168.4.3; 
    deny 31.42.145.0/24; 
    deny 51.12.35.0/24;
}

🎈 仅凋谢内网

  • 须要先禁止 192.168.1.1
  • 凋谢其余内网网段,而后禁止其余所有 IP
location / { 
  # block one workstation 
  deny    192.168.1.1; 
  # allow anyone in 192.168.1.0/24 
  allow   192.168.1.0/24; 
  # drop rest of the world 
  deny    all; 
}

🎈 负载平衡

  • 须要在 nginx.conf 中配置转发服务器信息
  • 权重: weight=1,权重如果调配的值越大,权重越高
  • 最大连接数: max_fails=3,最多连贯失败次数为3次
  • 连贯失败工夫: fail_timeout=20s,每次连贯失败的工夫
  • 在站点配置 default.conf 中开启负载平衡
# nginx.conf中配置转发服务器信息
upstream web {
    server 192.168.37.2 weight=1 max_fails=3 fail_timeout=20s;
    server 192.168.37.3 weight=1 max_fails=3 fail_timeout=20s;
}

# default.conf中开启负载平衡
location / {
    proxy_pass http://web/;
}

🎈 列出文件列表

  • 有时候服务器作为资源服务器,给用户提供下载资源应用
  • 须要将服务上的文件以目录模式列出来
  • 能够通过配置 autoindex on 容许列出目录,启用目录流量
  • 能够通过 autoindex_exact_size off 显示出文件的确切大小,单位是 bytes
  • 能够通过 autoindex_localtime on 显示的文件工夫为文件的服务器工夫
location / {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
}

🎈 路由转发

  • 有时候用户通过路由拜访服务器的资源,其实你的资源在另一个文件夹上面
  • 能够应用 alias 命令,将用户申请进行转发
# nginx服务器
location /static {
    alias /public;
}
 
# window服务器
location ^~ /static {
    alias "D:\\public\\动态资源";
}

🎈 开启 gzip 压缩

  • gzip 压缩是一种晋升访问速度的优化方向,能够大大提高
http {
    # 开启gzip
    gzip on;

    # 是否在http header中增加Vary: Accept-Encoding,倡议开启
    gzip_vary on;

    # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
    gzip_min_length 1k;

    gzip_proxied any;

    # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU工夫
    gzip_comp_level 6;

    # 设置压缩所须要的缓冲区大小
    gzip_buffers 16 8k;

    # 设置gzip的版本
    gzip_http_version 1.1;

    # 进行压缩的文件类型。javascript有多种形式,前面的图片压缩不须要的能够自行删除
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}

🎈 解决跨域

server {
    location / {
        #容许跨域申请的域,*代表所有 
        add_header 'Access-Control-Allow-Origin' *; 
        #容许带上cookie申请 
        add_header 'Access-Control-Allow-Credentials' 'true'; 
        #容许申请的办法,比方 GET / POST / PUT / DELETE 
        add_header 'Access-Control-Allow-Methods' *; 
        #容许申请的header 
        add_header 'Access-Control-Allow-Headers' *;
    }
}

🎈 资源防盗链

  • 为了避免其余网站间接实用我方的动态资源,能够减少防盗链配置
server {
    location ~*/(js|image|css) {
        # 检测*.autofelix.cn的申请,如果检测是有效的,间接返回403
        valid_referers *.autofelix.cn; 
        if ($invalid_referer) {
            return 403;
        }
     }
}

🎈 Keepalived 进步吞吐量

  • 通过 keepalived 能够设置长连贯解决的数量
  • 通过 proxy_http_version 能够设置长连贯 http 版本
  • 通过 proxy_set_header 能够革除 connection header 信息
# nginx.conf中配置吞吐量
upstream web {
    server 192.168.37.3 weight=1;keepalive 32;
}

# default.conf中配置
location / {
     proxy_pass http://tomcats;
     proxy_http_version 1.1;
     proxy_set_header Connection "";
}

🎈 HTTP 强制跳转 HTTPS

  • 很多网站中,都强制实用 https 协定
  • 这样咱们就须要将 http 强制跳转到 https
server {
    # 监听的端口号
    listen 80;
    
    # 强制跳转
    rewrite ^(.*)$ https://$host$1 permanent;
}
 
server {
    # 监听的端口号
    listen       443;
    # 主机名
    server_name www.520web.cn;
    # 开启ssl验证
    ssl on;
    # 字符集
    charset utf-8;
    # 拜访的根目录
    root   /var/www/html;
    # 谬误页面
    error_page  404    ...404文件门路;
    
    # 图片视频动态资源缓存到客户端工夫
    location ~ .*\.(jpg|jpeg|gif|png|ico|mp3|mp4|swf|flv){
      expires 10d;
    }
    
    # js/css动态资源缓存到客户端工夫
    location ~ .*\.(js|css){
      expires 5d;
    }
    
    # ssl的相干配置,pem文件的地址
    ssl_certificate  ...pem文件的绝对路径;
    # key文件的绝对路径
    ssl_certificate_key  ...key文件的绝对路径;
    # 断开重连工夫
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    # ssl协定
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    
    # 首页拜访的文件
    location / {
        index  index.php index.html index.htm;
    }

    # php-ftm配置
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理