侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

nginx新手快速入门及几种配置案例

技术  /  管理员 发布于 4年前   2883

nginx新手快速入门及几种配置案例


nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里。

一、每个域名一个文件的写法

首先打开nginx域名配置文件存放目录:/usr/local/nginx/conf/servers ,如要绑定域名www.xx.cc 则在此目录建一个文件:www.xx.cc.conf 然后在此文件中写规则,如:

server 
{ 

    listen  80; 
    server_name www.xx.cc;           #绑定域名 
    index index.htm index.html index.php;  #默认文件 
    root /home/www/xx.cc;           #网站根目录
    include location.conf;           #调用其他规则,也可去除

}

然后重起nginx服务器,域名就绑定成功了

nginx服务器重起命令:/etc/init.d/nginx restart

二、一个文件多个域名的写法

一个文件添加多个域名的规则也是一样,只要把上面单个域名重复写下来就ok了,如:

server 
{ 
    listen       80; 
    server_name www.xx.cc;             #绑定域名 
    index index.htm index.html index.php;  #默认文件 
    root /home/www/xx.cc;             #网站根目录
    include location.conf;                #调用其他规则,也可去除
}

server 
{ 
    listen       80; 
    server_name www.xx.cc;             #绑定域名 
    index index.htm index.html index.php;  #默认文件 
    root /home/www/msn.xx.cc;         #网站根目录
    include location.conf;                #调用其他规则,也可去除
}

三、不带www的域名加301跳转

如果不带www的域名要加301跳转,那也是和绑定域名一样,先绑定不带www的域名,只是不用写网站目录,而是进行301跳转,如:

server
{
    listen 80;
    server_name xx.cc;
    rewrite ^/(.*) http://www.xx.cc/$1 permanent;
}

四、添加404网页

添加404网页,都可又直接在里面添加,如:

server 
{ 
    listen       80; 
    server_name www.xx.cc;             #绑定域名 
    index index.htm index.html index.php;  #默认文件 
    root /home/www/xx.cc;             #网站根目录
    include location.conf;                #调用其他规则,也可去除
    error_page 404  /404.html; 
}

上面四种规则方法,基本就可以自己独立解决nginx配置基本问题了。

五:贴个更详细的(一个线上项目的nginx代理服务器配置)

limit_req_zone $www_spider zone=www:10m rate=6r/m;    
server {
listen       80;
server_name  www.xx.com;
include proxy.conf;
include sql_pc.conf;
limit_req zone=www burst=3 nodelay;
limit_req_status 444;
if ($http_referer ~* "duoqgfw.xyz") {
return 444;
}
if ($realip ~* "110.249.208.157|110.249.208.9|110.249.208.10|61.155.4.78|61.167.203.179|216.172.153.122|220.243.135.*|220.243.136.*") {return 444;}
if ($http_user_agent ~* "MegaIndex|GrapeshotCrawler|MJ12bot|BLEXBot|MauiBot|AhrefsBot|SemrushBot|Scrapy|YandexBot|DotBot"){return 444;}

location = /sendDataPc.js {root /nfs/ROOT/webresource/common/data-js;}
location ~ ^/(administrator513|email)/ {deny all;}
### gongjijin
location ~ ^/(apigjj|gongjijin|chagongjijin|gjjdk|gjjtq|gjjjc|gjjnews)/ {
set $log_tag 0;
if ($uri ~ .*\.(gif|jpg|png|ico|swf|jpeg|bmp|txt|js|css)$){
set  $log_tag 1;
}
if ( $log_tag = 1 ){
access_log off;
}
  proxy_pass http://gjj.xx.com;
}
rewrite  ^/loans/jisuanqi/(gjjjc|gjjedu)/  https://loans.xx.com/jisuanqi/$1/ permanent;

location ~* /card/checksms.asp  {
if ($request_uri ~* "rnd\=0.1525198888279058") {
set $a 1;
}
if ( $a = 1 ) { return 444;}
proxy_pass http://www.xx.com_server_pool;
}

rewrite "^/xml/newadcenter.asp?id=([0-9]+)$" https://www.xx.com/xml/js/js$1.js permanent;
rewrite ^/bank/default.asp$ https://www.xx.com/bank/ permanent;
rewrite ^/gongjijin$ https://www.xx.com/gongjijin/ permanent;
rewrite ^/bank/wangdian/(.*) https://www.xx.com/wangdian/$1 permanent;
rewrite ^/card/(beijing|hangzhou|bohai|chengdu|dalian|dongguan|fujianhaixia|fudian|guangzhou|guiyang|haerbin|hankou|hebei|huishang|jiangsu|jiangxi|jinshang|jiujiang|lanzhou|nanjing|neimenggu|ningbo|ningxia|qilu|qingdao|shanghai|shengjing|tianjin|wenzhou|changsha|zhejiangtailong|zheshang|zhengzhou|chongqing|jilin)/(.*)$  https://$host/card/$1yinhang/$2 permanent;

####---20191122----###
       rewrite  ^/chagongjijin/search.shtml$    https://www.xx.com/cardnews/cardnews_23563.shtml  permanent;

####20200605####
location =/ {
proxy_pass http://pcphp.xx.com;
proxy_set_header Host pcphp.xx.com;
}
       location / {  
root   html;  
index  index.html index.htm index.shtml;  
proxy_pass        http://www.xx.com_server_pool;
rewrite "/News/([a-zA-Z]+_[0-9]+).shtml$" https://www.xx.com/news/$1.shtml permanent;
rewrite "/wangdian/([a-zA-Z]+_[0-9]+)$" https://www.xx.com/wangdian/$1.shtml permanent;
rewrite "^/(home|top|youhui|ad)/(.*)" http://$1.xx.com/$2 permanent;
rewrite "/(.*)/target$" https://www.xx.com/$1/ permanent;
rewrite "/card/cardinfo/mintaibankxinyongka_([0-9]+).shtml" https://www.xx.com/card/cardinfo/mintaiyinhangxinyongka_$1.shtml permanent;
}  
location ~* /card/index.asp  {
if ($http_user_agent ~* "bingbot|sogou|yisou|Mozilla\/5.0\ \(Windows\ NT\ 6.1;\ Win64;\ x64\)\ AppleWebKit\/537.36\ \(KHTML,\ like\ Gecko\)\ Chrome\/69.0.3497.81\ Safari\/537.36"){return 444;}
if ($http_user_agent ~* "Mozilla\/5.0\ \(Windows\ NT\ 6.1;\ WOW64\)\ AppleWebKit\/537.36\ \(KHTML,\ like\ Gecko\)\ Chrome\/43.0.2357.81\ Safari/537.36"){return 444;}
if ($http_user_agent ~* "Mozilla\/5.0\ \(Windows\ NT\ 6.1;\ WOW64\)\ AppleWebKit\/537.36\ \(KHTML,\ like\ Gecko\)\ Chrome\/30.0.1599.101\ Safari/537.36"){return 444;}
if ($http_user_agent ~* "Mozilla\/5.0\ \(Linux;\ Android\ 6.0;\ Nexus\ 5\ Build\/MRA58N\)\ AppleWebKit\/537.36\ \(KHTML,\ like\ Gecko\)\ Chrome\/65.0.3325.181\ Mobile\ Safari\/537.36"){return 444;}
if ($http_user_agent ~* "sogou|yisou|spider") {
#return 444;
set $www_spider $http_user_agent;
}
if ($realip ~* "180.163.22?.*|42.236.10.*") {return 444;}
proxy_pass http://pcphp.xx.com;
           proxy_set_header Host pcphp.xx.com;
}
location ~* /sitemap/ {
  proxy_pass http://pcphp.xx.com;
  proxy_set_header Host pcphp.xx.com;
}
location ^~ /asset/ {
  proxy_pass http://pcphp.xx.com;
}
location ^~ /api/ {
  proxy_pass http://pcphp.xx.com;
}
location ~* (/card/lklsearch.asp)  {
if ($http_user_agent ~* "sogou|360spider|bing|yisou|baidu") {
return 444;
}
proxy_pass http://www.xx.com_server_pool;
       }

location ~* /wangdian/bankwdsearch.asp  {
if ($http_user_agent ~* "sogou|intel|360spider|bing|yisou") {
set $www_spider $http_user_agent;
}
if ($http_user_agent ~* "bingbot"){return 444;}
if ($realip ~* "180.163.22?.*|42.236.10.*") {return 444;}
proxy_pass http://www.xx.com_server_pool;
}

location ~* /wangdian/(.*)  {
if ($http_user_agent ~* "Mozilla\/5.0\(compatible;MSIE9.0;WindowsNT6.1;Trident\/5.0\)"){return 444;}
#agent:"Mozilla/5.0(compatible;MSIE9.0;WindowsNT6.1;Trident/5.0)"
if ($realip ~* "180.163.22?.*|42.236.10.*") {return 444;}
proxy_pass http://www.xx.com_server_pool;
}

location ~* ^/(images|eWebEditor_v280|kindeditor)/.*(gif|jpg|png|ico|swf|jpeg|bmp|txt|js|css) {
if ($http_referer  ~* "youhui.xx.com") {
           rewrite "/images/uploadimg/(.*).jpg" https://youhui.xx.com/images/nopics.jpg permanent;
rewrite "/images/uploadimg/(.*).png" https://youhui.xx.com/images/nopics.jpg permanent;
rewrite "/images/uploadimg/(.*).jpeg" https://youhui.xx.com/images/nopics.jpg permanent;
rewrite "/images/uploadimg/(.*).gif" https://youhui.xx.com/images/nopics.jpg permanent;
rewrite "/images/uploadimg/(.*).bmp" https://youhui.xx.com/images/nopics.jpg permanent;
       }
proxy_pass http://www.xx.com_server_pool;
access_log logs/www_tp.log test;
expires 7d;
}
###---20190521新版卡中心配置  ----
location ~ ^/(bank|baoshang|zhonghang|zhongxin|chongqingyinhang|zjnongshang|hrxjbank|mintaiyinhang)/$ {
proxy_pass http://cbb-pc-web.xx.com;
}
location ~ ^/webresource/ {
proxy_pass http://cbb-pc-web.xx.com;
access_log logs/www_tp.log test;
expires 7d;
}
###
access_log logs/www_access.log  test;
error_page  404 https://www.xx.com/error.asp;
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}
location = /check.txt{
root /nfs/ROOT;
access_log off;
}
}



  • 上一条:
    PHP封装返回Ajax字符串和JSON数组的方法
    下一条:
    laravel之编写守护进程supervisor使用说明详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客