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

管理员 发布于 3年前   405

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;
}
}


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客