nginx 单机实现多网站,负载均衡,upstream

nginx 是一款基于epoll机制的web服务
注意安装nginx的时候需要安装一个pcre-devel,这个模块主要是
讲下nginx的基本使用:
nginx -t 测试nginx的安装与配置是否正确,相当于是一个语法的检查
nginx -s reload 就是一个重新加载配置的操作
nginx配置文件是分块实现配置的

我们可以通过配置nginx.conf来实现我们单台主机上跑三个网页:

worker_process 1; worker进程的数量
events {
work_connection 1024;
}
http {
    include       mime.types; #媒体资源库
    default_type  application/octet-stream;
    sendfile        on;#开启高校传输
    keepalive_timeout  65;#链接超时 连接着不干事了就断开
    server {   #网站设置
        listen       80;
        server_name  www.blog.com; #服务域名主机名
        location / {
            root   html/blog;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ *.(gif|jpg|png|bmp){
		proxy_pass  http://static_pools;
		include proxy.conf;
		}
    }
	  server {   #网站设置
        listen       80;
        server_name  www.bbs.com; #服务域名主机名
        location / {
            root   html/bbs;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
        }
        if ($http_host ~* "^etiantian.org$"){	
		rewrite ^/(.*) http://bbs.etiantian.org/$1 permant;
		#解释下(.*) 就是匹配到的$host,将匹配到的永久的跳转到http://bbs.etiantian.org/$1  $1是正则匹配到的东西,就是从^直到/,然后后面的内容就是匹配的内容$1
	}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    }

proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connection_timeout 60;
实现了单主机跑两个网络服务
www.bbs.com
www.blog.com
同时你需要在你的windows机器上配置好你的dns解析
检查连接是否正常

curl -I -s www.baidu.com|head -1|tr " " "\n")
返回的是数组
#[root@localhost ~]# curl -I -s  www.baidu.com|head -1|tr " " "\n"HTTP/1.1
#200
#OK
. /etc/init.d/functions
function checkurl(){
checkUrl=$1##注意千万别在括号两端加空格
judge=($(curl -I -s www.baidu.com|head -1|tr " " "\n"))
if [[ "${{judge[1]}}" == '200' && ${{judge[2]}} == 'ok' ]]
then
	action "$checkurl" /bin/true
else
	action "$checkurl" /bin/false
fi
}
checkurl www.baidu.com

同时我们可以使用include的方式使得nginx.conf文件看上去更加简洁
注extra在conf下

http{
include mime.types;
include extra/www.conf;
include extra/bbs.conf
}

www.conf

 server {   #网站设置
        listen       80;
        server_name  www.blog.com; #服务域名主机名
        location / {
            root   html/blog;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

nginx -c /application/nginx/conf/nginx.conf
虚拟主机别名:www.blog.com blog.com都到同一个网站
有点类似rewrite 就是访问

 server {   #网站设置
        listen       80;
        server_name  www.blog.com
		blog.com; #服务域名主机名
        location / {
            root   html/blog;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

ngx_http_stub_status_module 是记录nginx状态的模块

server {
listen       80;
        server_name  www.blog.com
		blog.com; #服务域名主机名
        location / {
            root   html/blog;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
            stub_status on;
            access-log off;
        }
}

错误日志:

error_log logs/error.log **error**; error是记录错误日志的等级

nginx负载均衡:

worker_process 1; worker进程的数量
events {
work_connection 1024;
}
http {
    include       mime.types; #媒体资源库
    default_type  application/octet-stream;
    sendfile        on;#开启高校传输
    keepalive_timeout  65;#链接超时 连接着不干事了就断开
    upstream www_pools {
	server 10.0.0.7:80 weight=1;
	server 10.0.0.8:80 weight=1;
	}
	
    server {   #网站设置
        listen       80;
        server_name  https://www.bbs.com; #服务域名主机名
        location / {
            root   html;#站点目录用户要的文件信息
            index  index.html index.htm;#首页文件默认
            proxy_pass http://www_pools#就是访问https://www.bbs.com分配给7或者8主机,实现负载均衡weight是权重
        }

    }
    }

nginx 日志分割:

DateFormt=date +%Y%m%d  -d -1day
BaseDir=/application/nginx
NginxDir="$BaseDir/logs"
Logname="access_www"
[ -d $NginxDir ] && cd NginxDir  ||exit 1
[ -f ${Logname}.log ]  ||exit 1
/bin/mv $ ${Logname}.log ${DateFormt}_${Logname}.log
crontab -e
0 0 * * *  /bin/bash seperate.sh

server {
listen 80;
server_name http://www.bbs.com
location /imges {
return 500;
}
}

rewrite 语法:
rewrite regx replacement [flag]
#permanent 永久跳转
rewrite ^/(.*) http://www.baodu.com/$1 permanent;

server {
listen 80;
server_name http://www.bbs.com
location / {
root html;
index index.html,index.htm;
}
if ( $http_host ~* "^(.*)\.etiantian\.org$" ){
	set $domain $1;
	rewrite ^(.*) http://www.etiantian.org/$domain/oldsix.html break
	}
}

upstream 模块 ip_hash 就是保存之前的访问不会负载均衡了,访问的谁,以后访问还是那台机器
http{
server_name www.baixx.com;

upstream pools {
ip_hash;
server 10.0.0.7:80;
server 10.0.0.8:80;
}

location / {
root html;
proxy_pass http://pools;
proxy_set_header Host $host; #在http请求头中加入host字段信息,用于后端配置了多台服务器负载均衡的时候,知道是到的那一台
proxy_set_header X-forward-For $remote_addr;#解决日志总是记录反向代理机器的ip,现在记录的就是那台访问的而不是反向代理那台
}

}
若大佬有指教请留言比如更多的模块讲解谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值