fifth_nginx入门与实战_负载均衡

 

1.nginx入门

  1.为什么要使用nginx:

    开源的,支持高性能,高并发的www服务和代理服务软件

    占用的系统资源更少,可反向代理、负载均衡、以及缓存服务

    3万并发连接下开启10个nginx线程消耗的内存不到20M

  2.安装

# 安装nginx需要的依赖库
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

# 下载源码包
cd /opt  # 下载到opt下
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

# 解压文件(/opt目录下)
tar -zxvf nginx-1.12.0.tar.gz

# 配置,编译安装,开启nginx状态监测功能
cd /opt/nginx-1.12.0
./configure --prefix=/opt/nginx112   # nginx112为命名

# 编译安装

cd /opt/nginx-1.12.0
make && make install  # &&(and)执行前面的再执行后面的

# 启动nginx,进入sbin目录 找到nginx启动命令
cd /opt/nginx112/sbin
./nginx  # 启动
./nginx -s stop  # 关闭
./nginx -s reload  # 平滑重启,修改了nginx.conf之后,可以不重启服务,加载新的配置

# 查看nginx运行状态
netstat -tunlp  # 查看端口是否运行,一般为80端口
ps -ef | grep nginx  # 查看进程是否已运行

# nginx112目录下的文件
conf 存放nginx所以配置文件的目录,常用nginx.conf
html 存放nginx默认站点的目录,常用index.html、error.html等
logs 存放nginx默认日志的目录,常用error.log access.log
sbin 存放nginx注明了的目录,常用sbin/nginx
# nginx112.conf 配置文件解析
worker_processes  5;  #定义nginx工作进程数
#error_log  logs/error.log;  #错误日志
#http定义代码主区域
http {
    include       mime.types;
    default_type  application/octet-stream;
    #定义nginx的访问日志功能
    #nginx会有一个accses.log功能,查看用户访问的记录
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;  #开启日志功能
    sendfile        on;
    keepalive_timeout  65;
    #开启gzip压缩传输,使传输更快
    gzip  on;
    #  自定义一个 server,你也可以用原有的 
       server {
        listen       80;  # 默认一般为80端口
        server_name  benben.qishi3dj.com;  # 定义网站域名
        location / {
            root   /opt/qishi3dj/benben;   # 定义网页根目录(mkdir -p /opt/qishi3dj/benben然后touch /opt/qishi3dj/benben/index.html,然后随便写点)
            index  index.html index.htm;  # 获取目录下的html文件返回
        }
        # 连接失败时返回的页面,如40x报错:192.168.1.56/wqeqesdaa
error_page 400 402 403 404 /40x.html; # 报错html必须跟上面的html的同一路径,因为找的是绝对路径 error_page
500 502 503 504 /50x.html;
# 你也可以通过以下方法定位绝对路径 # location
= /50x.html { # root html; # } }

nginx启动服务器时,发现报nginx: [error] open() “/alidata/server/nginx/logs/nginx.pid” failed (2: No such file or directory)错误,进到logs文件发现的确没有nginx.pid文件

# 解决方法,我的是/opt下nginx112,-c指定文件位置
/opt/nginx112/sbin/nginx -c /opt/nginx112/conf/nginx.conf
# 再去 nginx下logs/
ll  # 即可查询到已有文件

2.nginx访问日志:对每个访问用户进行记录

log_format    记录日志的格式,可定义多种格式
accsss_log    指定日志文件的路径以及格式


  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
# 在nginx112下
tail -f logs/access.log  # 即可实时查看打印到显示器的登陆用户

3.限制网站来源的IP访问

4.配置nginx多虚拟主机:达到可以通过不同域名,显示不同网站

  1.修改配置文件

# nginx.conf
   worker_processes  5;

#error_log  logs/error.log;

events {
   worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  zongze.qishi3.com;
        location / {
            #deny 192.168.1.0/24;
            root   /opt/qishi3/zongze;
            index  index.html index.htm;
        }
        error_page   400 402 403 404  /40x.html;
        #error_page   500 502 503 504  /50x.html;
    }
    server {
        listen       80;
        server_name  xiaochun.qishi3.com;
        location / {
            root   /opt/qishi3/xiaochun;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  huafeng.qishi3.com;
        location / {
            root   /opt/qishi3/huafeng;
            index  index.html index.htm;
        }
    }
}

  2.在服务器上创建三个目录

cd /opt
rm -rf qishi3
mkdir -p qishi3/huafeng
mkdir -p qishi3/zongze
mkdir -p qishi3/xiaochun
touch qishi3/huafeng/index.html
touch qishi3/zongze/index.html
touch qishi3/xiaochun/index.html
分别打开这三个index.html文件
vim index.html
分别添加内容
我是华峰
我是宗泽
我是小春
然后保存退出

  3.修改本机hosts文件

c:\\windows\system32\drivers\etc\hosts  # 路径
# 用 not++ 或者管理模式的记事本,添加以下三行解析记录
192.168.1.40    zongze.qishi3.com
192.168.1.40    xiaochun.qishi3.com
192.168.1.40    huafeng.qishi3.com
# 平滑重启nginx

 

5.负载均衡+代理分发:访问多个不同域名,显示三个网站

# 配置代理服务器,vim nginx112/conf/nginx.conf,转发名字对应upstream 名字

都平滑重启即可

posted @ 2019-04-02 19:32  pythonernoob  阅读(165)  评论(0)    收藏  举报