✨ 目录 🎈 封禁 IP🎈 仅开放内网🎈 负载均衡🎈 列出文件列表🎈 路由转发🎈 开启
✨ 目录
deny
可以封禁指定 IP
Http {# ....# 封禁IPdeny 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
压缩是一种提升访问速度的优化方向,可以大大提高访问速度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
可以设置长连接处理的数量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 "";}
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; }}
来源地址:https://blog.csdn.net/weixin_41635750/article/details/125857045
--结束END--
本文标题: 超实用 nginx 中常见的配置合集
本文链接: https://www.lsjlt.com/news/393600.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-11
2024-05-11
2024-05-10
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0