新开网站进行nginx设置(nginx一键设置)

# 屏蔽恶意蜘蛛
if ($http_user_agent ~* "hubspot|CCBot|VelenPublicWebCrawler|Konturbot|my-tiny-bot|eiki|webmeup|ExtLinksBot|Go-http-client|Python|ZoominfoBot|MegaIndex.ru|MauiBot|Amazonbot|ds-robot|intelx.io|coccocbot|FeedDemon|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|CoolpadWebkit|Java|Barkrowler|Feedly|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|DuckDuckGo|ClaudeBot|coccocbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|MJ12bot|DotBot|heritrix|Bytespider|BLEXBot|serpstatbot|Ezooms|JikeSpider|Barkrowler|InfoTigerBot|SemrushBot|DuckDuckGo-Favicons-Bot|ImagesiftBot|GPTBot"){
return 403;
}
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md|package.json|package-lock.json|\.env|\.zip|\.tar\.gz) {
return 404;
}
# 解决特殊字体不显示的问题
location ~ ^/(\.eot|\.ttf|\.ttc|\.otf|\.eot|\.woff|\.woff2|\.svg) {
add_header Access-Control-Allow-Origin *;
}
# 1.强制把80端口转向443 , 该处和下面的2处是不可互换的,先跳转到https, 再转向www
#if ($server_port !~ 443){
# rewrite ^(/.*)$ https://$host$1 permanent;
#}
#HTTP_TO_HTTPS_START
set $isRedcert 1;
if ($server_port != 443) {
set $isRedcert 2;
}
if ( $uri ~ /\.well-known/ ) {
set $isRedcert 1;
}
if ($isRedcert != 1) {
rewrite ^(/.*)$ https://$host$1 permanent;
}
# HTTP_TO_HTTPS_END
# 2.把不带www的转向到www上来
#if ($host = "kd68.cn"){
# rewrite ^(/.*)$ https://www.$host$1 permanent;
#}
# 也可以如下这样写
if ($host !~ www){
rewrite ^(/.*)$ https://www.$host$1 permanent;
}
# 3.原来百度收录的文章 如: https://www.hao366.net/tags-etagid6584-0.html 设置成能正常访问
if ($uri ~* "/tags-etagid|/tougao/|/wenda/|/post/"){
rewrite ^(/.*)$ https://$host permanent;
}
# 4.设置url中含有/abc/形式的禁止访问
if ($uri ~* "/abc/"){
return 403;
}
#location / {
## 你的其他配置 ...
## 禁用缓存,这里是设置禁止使用缓存的,如果需要,可以打开注释
# add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
# expires off;
# etag off;
#}如果新增一个网站,一般情况下https是必须使用的, 但还要把 类似 http://www.hao366.net , https://hao366.net 这样的网址都统一转向到 https://www.hao366.net , 经本人测试,是有顺序的, 按照上面1、2顺序加入即可,如果换了,网址会跳转多次,浪费流量。
对图片及css和js设置客户端缓存
#对图片设置缓存
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
add_header Cache-Control "public";
try_files $uri =404;
error_log /dev/null;
access_log /dev/null;
}
#对css/js设置缓存
location ~ .*\.(js|css)?$
{
#expires 12h;
expires 30d;
add_header Cache-Control "public";
try_files $uri =404;
error_log /dev/null;
access_log /dev/null;
}
















