Analysis of Nginx common configuration examples

WBOY
Release: 2023-06-02 23:14:50
forward
1273 people have browsed it

封禁 IP

通过 deny 可以封禁指定 IP

http {
    # ....
    # 封禁IP
    deny 192.168.4.3; 
    deny 31.42.145.0/24; 
    deny 51.12.35.0/24;
}
Copy after login

仅开放内网

需要先禁止 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; 
}
Copy after login

负载均衡

需要在 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/;
}
Copy after login

列出文件列表

有时候服务器作为资源服务器,给用户提供下载资源使用

需要将服务上的文件以目录形式列出来

可以通过配置 autoindex on 允许列出目录,启用目录流量

可以通过 autoindex_exact_size off 显示出文件的确切大小,单位是 bytes

可以通过 autoindex_localtime on 显示的文件时间为文件的服务器时间

location / {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
}
Copy after login

路由转发

有时候用户通过路由访问服务器的资源,其实你的资源在另一个文件夹下面

可以使用 alias 命令,将用户请求进行转发

# nginx服务器
location /static {
    alias /public;
}
 
# window服务器
location ^~ /static {
    alias "D:\\public\\静态资源";
}
Copy after login

开启 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;
}
Copy after login

解决跨域

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' *;
    }
}
Copy after login

资源防盗链

为了防止其他网站直接实用我方的静态资源,可以增加防盗链配置

server {
    location ~*/(js|image|css) {
        # 检测*.autofelix.cn的请求,如果检测是无效的,直接返回403
        valid_referers *.autofelix.cn; 
        if ($invalid_referer) {
            return 403;
        }
     }
}
Copy after login

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 "";
}
Copy after login

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;
    }
}
Copy after login

The above is the detailed content of Analysis of Nginx common configuration examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!