1.背景
需求: nginx分发6个tomcat
2.nginx 安装
非容器安装
1. yum安装
## 准备工作安装 openssl zlib pcre
yum -y install openssl zlib pcre
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum -y install nginx
2. 启动
service nginx start
--Redirecting to /bin/systemctl start nginx.service
## 自启
systemctl enable nginx
3. selinux设置
查看是否开启
selinux sestatus -v
/usr/sbin/sestatus -v
### 若显示SELinux status: enabled代表开启了selinux
永久关闭
vi /etc/selinux/config
## 将SELINUX=enforcing改为SELINUX=disabled
## 设置完成后重启服务才生效
reboot
4. 优化
开启Gzip
## 在nginx.conf文件中修改或增加如下内容
gzip on;
gzip_http_version 1.1;
gzip_comp_level 3;
gzip_types text/css
application/javascript text/plain application/json application/x-javascript
application/css application/xml application/xml+rss text/javascript
application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
修改上传附件限制
#### 在/etc/nginx/nginx.conf文件中修改或增加如下内容
client_max_body_size 100M;
修改请求超时设置
#### 在/etc/nginx/nginx.conf文件中修改或增加如下内容
proxy_read_timeout 240s;
修改启动用户
#### 在/etc/nginx/nginx.conf文件中修改
user nginx;
## 改为
user root;
- 查看nginx是否启动成功
- ps -ef|grep nginx
5.配置tomcat分发
修改 nginx.conf 文件:
include /etc/nginx/conf.d/*.conf;
upstream serverpool{
ip_hash;
server 192.168.18.146:8001;
server 192.168.18.146:8002;
server 192.168.18.146:8010;
server 192.168.18.147:8001;
server 192.168.18.147:8002;
server 192.168.18.147:8010;
}
server {
listen 8899;
server_name localhost;
location / {
# proxy_pass http://serverpool;
proxy_pass http://serverpool/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 6500s;
proxy_read_timeout 6500s;
proxy_send_timeout 6500s;
}
}
}
6.tips
# nginx 日志路径:
/var/log/nginx
# 重启nginx:
/bin/systemctl restart nginx.service
# 查看nginx的状态
systemctl status nginx.service
netstat -anp|grep nginx