Nginx安装及使用

Nginx安装使用及高可用

一.nginx安装

1.创建一个文件夹,存放安装的nginx文件
mkdir /nginx
2.安装nginx依赖包
yum -y install gcc gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel
3.解压下载好的nginx源码包
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
4.配置编译并安装
./configure --prefix=/nginx
make
make install
5.启动nginx
/nginx/sbin/nginx
6.设置开机自启
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking
ExecStart=/nginx/sbin/nginx
ExecReload=/nginx/sbin/nginx -s restart
ExecStop=/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
7.关闭nginx设置开机自启
systemctl enable nginx.service
systemctl start nginx
8.nginx进程模型
master	主进程
master用于管理worker
worker	工作进程
worker是为master进行服务的

二.Nginx配置文件

1.nginx.conf配置结构
main	全局配置
	event	连接数
	http	http模块配置
		server	具体的服务
		location	路由规则表达式
		upstream	集群,内网服务器
2.默认配置文件内容
#指定操作系统的哪一个用户来执行
#user  nobody;
#指定工作进程
worker_processes  1;
#错误日志
#debug info notice warn error crit
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#进程id
#pid        logs/nginx.pid;

#设置最大连接数
events {
    worker_connections  1024;
}


http {
	#导入外部文件   可以解析的一些文件类型
    include       mime.types;
    #默认type类型
    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;
    #当数据包累计多少大小后发送
    #tcp_nopush     on;
	#客户端连接服务端超时时间,连接可以保持多长时间
    #keepalive_timeout  0;
    keepalive_timeout  65;
	#传输内容是否压缩
    #gzip  on;

    server {
    	#端口号
        listen       80;
        #服务访问地址/方式
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		#路由,需要取找的页面 斜杠代表根
        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #页面发生错误时调出页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

三.Nginx日志切割

1.手动
#!/bin/bash
log_path="/nginx/logs/"
recode_time=$(date -d "yesterday" +%y-%m-%d+%H:%M)
pid=/nginx/logs/nginx.pid
mv ${log_path}/access.log ${log_path}/accwss.${recode_time}.log
mv ${log_path}/error.log ${log_path}/error.${recode_time}.log
kill -USR1 `cat $pid`
2.自动
crontab -e
*/1 * * * * /nginx/sbin/qg_log.sh

四.静态资源服务

1.举例
 server {
    	#端口号
        listen       81;
        #服务访问地址/方式
        server_name  localhost;
		#路由,需要取找的页面 斜杠代表根
        location / {
            root   html;
            index  index.html index.htm;
        }
        #访问多个
        location /www {
            root   /home;
        }
        #别名方式
        location /start {
            alias   /home/www;
        }

五.gzip

1.gzip配置
#开启gzip压缩功能,提高传输效率,减少带宽开销
gzip on;
#限制最小压缩,小于1个字节就不压缩
gzip_min_length 1;
#定义压缩级别
gzip_comp_level 3;
#定义压缩文件类型
gzip_types text/plain text/css;

六.Nginx静态资源防盗链

1.配置nginx防盗链
#对原站点验证
valid_referers none *.heber.com;
#非法引入会进入下方判断
if ($invalid_referer){
	return 404;
}

七.nginx集群Tomcat

1.集群配置
#配置上游服务器
upstream heber{
	server	192.168.0.118:8080;
	server	192.168.0.119:8080;
}

server {
        listen	80;
        server_name  www.he_ber.com;
        location / {
            proxy_pass	http://heber;
        }
}

2.upstream参数指令
max_conns	最大连接数							slow_start	让集群缓慢启动
down	不参与负载								backup	表示备用机
max_fails	最大失败次数							fail_timeout	失败时间段
3.配置加权轮巡
upstream heber{
	server	192.168.0.118:8080	weigth=3;
	server	192.168.0.119:8080	weigth=1;
}

八.nginx控制缓存

1.浏览器缓存
expires	10s;
expires	@22h30m;
2.上游服务器缓存
#配置上游服务器
upstream heber{
	server	192.168.0.118:8080;
	server	192.168.0.119:8080;
}
#设置缓存保存的目录
proxy_cache_path /nginx/upsteam_cache keys_zone=mycache:5m max_size=1g inactive=1h use_temp_path=off;
server {
        listen	80;
        server_name  www.he_ber.com;
        #开启并使用缓存
        proxy_cache	mycache;
        #设置针对缓存码的缓存时间
        proxy_cache_valid 200 304 8h;
        location / {
            proxy_pass	http://heber;
        }
}

九.nginx高可用

1.安装keepalived
#解压安装包
tar -zxvf keepalived-2.0.18.tar.gz
#创建一个文件夹存放安装的应用
mkdir /nginx/keepalived
#安装依赖包
yum -y install libnl libnl-devel
#配置keepalived安装路径
./configure --prefix=/nginx/keepalived --sysconf=/etc
#开始安装
make && make install
#查看keepalived在什么位置
whereis keepalived
2.配置keepalived
#打开配置文件
vim /etc/keepalived/keepalived.conf
#配置参数
! Configuration File for keepalived
#如果出现问题邮箱通知配置
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   #路由id:当前安装keepalived的主机节点标识符
   #router_id LVS_DEVEL
   router_id keep_118
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
#加入nginx检测脚本
vrrp_script check_nginx_alived {
	script	"/etc/keepalived/check_nginx_alivd_or_not.sh"
	interval 2
	weigth 10
}
#计算机节点
vrrp_instance VI_1 {
	#表示的状态,当前的118为nginx的主节点,MASTER/BACKUP
    state MASTER
    #当前实例绑定的网卡
    interface ens33
    #保证主备节点一致
    virtual_router_id 51
    #权重,优先级
    priority 100
    #主备之间同步检查的时间
    advert_int 1
    #认证授权的密码
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    #调用脚本
    track_script {
    	check_nginx_alived
    }
    #虚拟ip
    virtual_ipaddress {
        192.168.0.120
    }
}
3.启动keepalived
cd /nginx/keepalived/sbin
./keepalived
4.设置开机自启
cd /opt/keepalived-2.0.18/keepalived/etc
cp init.d/keepalived /etc/init.d/
cp sysconfig/keepalived /etc/sysconfig/
systemctl daemon-reload
systemctl enable keepalived
5.备用节点配置
! Configuration File for keepalived
global_defs {
   #路由id:当前安装keepalived的主机节点标识符
   router_id keep_119
}  
#计算机节点
vrrp_instance VI_1 {
        #表示的状态,当前的118为nginx的主节点,MASTER/BACKUP
    state BACKUP
    #当前实例绑定的网卡
    interface ens33
    #保证主备节点一致 
    virtual_router_id 51
    #权重,优先级
    priority 80
    #主备之间同步检查的时间
    advert_int 1 
    #认证授权的密码
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    #虚拟ip
    virtual_ipaddress {
        192.168.0.120
    }
}

6.配置keepalived重启nginx
vim check_nginx_alivd_or_not.sh
#!/bin/bash
ng=`ps -C nginx --no-header |wc -l`
#判断是否宕机,如果宕机自动重启
if [ $ng -eq 0 ];then
	/nginx/sbin/nginx
	#等待一会继续检查
	sleep 3
	if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	killall keepalived
	fi
fi

7.双主热备
#在之前的备用服务里面添加
vrrp_instance VI_2 {
    state MASTER
    interface ens3
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.121
    }
}
#在之前的主服务里面添加
vrrp_instance VI_2 {
    state BACKUP
    interface ens33
    virtual_router_id 52
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.121
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值