linux 下 nginx 安装部署,反向代理tomcat等应用服务

一、准备

nginx 1.16.1

二、编译安装

安装nginx编译安装的依赖软件包

[root@pve-97 nginx]# yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

 上传nginx安装包至相应目录,这里我们指定 /cjy/nginx

[root@pve-97 ~]# ll /ng/nginx/
total 1012
-rw-r--r--. 1 root root 1032630 Jan  8 14:35 nginx-1.16.1.tar.gz

解压安装包

[root@pve-97 nginx]# tar -zxvf nginx-1.16.1.tar.gz

解压后目录如下:

[root@pve-97 nginx-1.16.1]# ll
total 752
drwxr-xr-x. 6 1001 1001   4096 Jan  8 14:47 auto
-rw-r--r--. 1 1001 1001 296463 Aug 13 20:51 CHANGES
-rw-r--r--. 1 1001 1001 452171 Aug 13 20:51 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 Jan  8 14:47 conf
-rwxr-xr-x. 1 1001 1001   2502 Aug 13 20:51 configure
drwxr-xr-x. 4 1001 1001     72 Jan  8 14:47 contrib
drwxr-xr-x. 2 1001 1001     40 Jan  8 14:47 html
-rw-r--r--. 1 1001 1001   1397 Aug 13 20:51 LICENSE
drwxr-xr-x. 2 1001 1001     21 Jan  8 14:47 man
-rw-r--r--. 1 1001 1001     49 Aug 13 20:51 README
drwxr-xr-x. 9 1001 1001     91 Jan  8 14:47 src
  • auto:存放大量脚本,与根目录configure文件相关;
  • conf:存放nginx的配置文件;
  • html:存放nginx首页面和其他的html页面;
  • man:存放nginx的的帮助文档,安装文成后可以使用man命令查看帮助;
  • src:存放nginx源代码
  • configure文件:脚本文件,做一些准备工作,包括系统内核检测、必须软件库检测、参数解析、中间目录生成,生成makefile文件等等

执行 ./configure 检查并配置

[root@pve-97 nginx-1.16.1]# ./configure 
checking for OS
 + Linux 3.10.0-1062.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found

...

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

得到配置概要以后,就可以直接编译和安装了 make & make install

安装完成后,nginx被默认安装在 /usr/local/nginx 目录

 

三、nginx开机启动

创建文件nginx vi /lib/systemd/system/nginx.service

具体内容如下:

[Unit]  
Description=nginx  
After=network.target  
   
[Service]  
Type=forking  
ExecStart=/usr/local/nginx/sbin/nginx 
ExecReload=/usr/local/nginx/sbin/nginx -s reload  
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true  
   
[Install]  
WantedBy=multi-user.target

设置开机启动 systemctl enable nginx.service

[root@pve-97 init.d]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

使用服务命令启动 systemctl start nginx

四、nginx Tomcat 负载均衡配置

打开路径 /usr/local/nginx/conf/nginx.conf 编辑nginx配置文件

找到 http 模块配置段

...略
http {
    ...略
    
    # 增加这个配置 (在这儿配置多个服务器)
    upstream servers_uias {
        # ip_hash指令,将同一用户引入同一服务器。
        #ip_hash;
        server      127.0.0.1:801 weight=1; #weigth参数表示权值,权值越高被分配到的几率越大。详细配置可见下章表格
        server      127.0.0.1:802 weight=1;
        server      127.0.0.1:803 weight=1;
    }
    
    #修改server
    server{
        # nginx监听80端口
        listen      80;
      # 特别注意server_name配置,这儿在实际使用中配置多个域名,比如test.com,www.test.com。
        server_name uias.ng.com.cn 192.168.1.91;
        location / {
            root    html;
            index   index.html;
            proxy_pass      http://servers_uias; #这里http://后面指向增加的upstream
            client_max_body_size    100m;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Real-PORT $remote_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    ...略
}
...略

upstream的server标签参数说明

  • server 127.0.0.1:801

负载均衡后面的RS配置,可以是IP或域名,如果端口不写,默认是80端口。高并发场景下,IP可以换成域名,通过DNS做负载均衡。

  • weight

代表服务器的权重,默认值是1.权重数字越大表示接受的请求比例越大。 max_fails=1|nginx尝试连接后端主机的次数,这个数值是配合proxy_next_upstream,fastcgi_next_upstream和memcached_next_upstream这三个参数来使用,当nginx接收后返回这三个参数定义的状态码时,会将这个请求转发给正常工作的后端服务器,列如404,502,503,max_fails的默认值是1;企业场景:建议2-3次,京东1次,蓝汛10次(CDN),根据业务需求去配置。

  • fail_timeout

在max_fails定义的失败次数后,距离下次检查的间隔时间,默认是10s,如果max_fails是5,它就检测5次,如果5次都是502。那么,他就会根据fail_timeout的值,等待10s再去检查,还是只检查一次,如果持续502,在不重新加载nginx配置的情况下,每隔10s都只检测一次,常规业务2-3秒比较合理,比如京东3秒,蓝汛3秒,可根据业务需求去配置。

  • backup

热备配置(RS节点的高可用),当前面激活的RS都失败后会自动启用热备RS,这标志这个服务器作为备份服务器,若主服务器全部宕机了,就会向他转发请求;注意,当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能使weight和backup。

  • down

表示单前的server暂时不参与负载。

修改完成后 使用命令/usr/local/nginx/sbin/nginx -s reload 或 systemctl restart nginx

五、nginx的操作

启动

1、直接执行二进制程序:

[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# pwd
/usr/local/nginx
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]#
[root@iZwz9g2hqiikgs5lncf7f7Z nginx]# ./sbin/nginx

这时,会读取nginx安装目录下的配置文件:/usr/local/nginx/conf/nginx.conf

2、指定配置文件的方式启动:

/usr/local/nginx/sbin/nginx –c /tmp/nginx.conf

这时,会读取-c参数后指定的nginx.conf配置文件来启动nginx。

3、指定安装目录的方式启动

/usr/local/nginx/sbin/nginx –p /usr/local/nginx/

4、指定全局配置项的启动方式

/usr/local/nginx/sbin/nginx –g “pid /var/nginx/test.pid”

这意味着nginx的pid文件会写入到指定的目录。-g参数不能与默认路径下的nginx.conf配置冲突,否则无法成功启动。

停止nginx

1、 快速停止 查找进程 ps -ef | grep nginx 杀掉进程 kill –s SIGTERM 10800 或 kill –s SIGINT 10800

2、使用stop命令 ./sbin/nginx –s stop

当快速停止服务时,worker进程与master进程在收到信号后会立刻跳出循环,退出进程。

3、平滑停止 停止master进程: ./sbin/nginx –s quit 等同于 kill -s SIGQUIT 停止work进程:kill -s SIGWINCH

平滑停止服务时,首先会关闭监听端口,停止接收新的连接,然后把当前正在处理的连接全部处理完,最后再退出进程。

重新加载配置文件

/usr/local/nginx/sbin/nginx -s reload

Nginx会先检查新的配置项是否有误,如果全部正确就以“优雅”的方式关闭,再重新启动Nginx来实现这个目的。类似的,-s是发送信号,仍然可以用kill命令发送HUP信号来达到相同的效果。 kill -s SIGHUP

日志文件回滚

使用-s reopen参数可以重新打开日志文件,这样可以先把当前日志文件改名或转移到其他目录中进行备份,再重新打开时就会生成新的日志文件。这个功能使得日志文件不至于过大。例如: /usr/local/nginx/sbin/nginx -s reopen 当然,这与使用kill命令发送USR1信号效果相同。kill -s SIGUSR1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lucky_m_fish

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值