注意:本次是在一台服务器中,安装Nginx配置多个Tomcat【多台服务器,原理其实类似,多个Tomcat相当于就是多台服务器】
配置之前,必须要做的一件事就是,搞清楚
1、什么是Nginx?
2、为什么要用Nginx?
3、Nginx优缺点
弄明白之后,配置起来的效率会提升很多
下载地址:http://nginx.org/download/
yum install perl
yum install gcc
yum install gcc-c++
yum -y install net-tools
1、解压
tar -zxvf nginx-1.15.8.tar.gz
2、进入【/usr/local/nginx-1.15.8】目录,进行编译【root身份依次执行如下命令】
./configure
make
make install
输入命令【./configure】可能出现以下两个错误。我两个都遇到了。。。。。。
第一个:
解决方法:
yum -y install pcre-devel
第二个:
解决方法:
yum -y install openssl openssl-devel
编译完成后,在【/usr/local】目录下会看到两个文件夹:【nginx-1.15.8】和【nginx】
进入【/usr/local/nginx/sbin】目录
1、启动(先关闭80端口tomcat)
./nginx
2、立即停止
./nginx -s stop
3、正常停止
./nginx -s quit
4、重新加载【nginx.conf】配置文件
./nginx -s reload
5.配置软链接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
然后就可以直接输入命令
a、启动nginx
nignx
b、停止nginx
正常停止
nginx -s quit
强制停止
nginx -s stop
c、重启nginx
nginx -s reload
1、查看Nginx进程
ps -ef|grep nginx
2、Linux输入以下命令,控制台如图显示,
curl -i 127.0.0.1
3、浏览器输入192.168.31.53 即 http://192.168.31.53:80【浏览器默认自带80端口】
注意:如果安装完成,查询Nginx进程正常,本机输入命令【curl -i 127.0.0.1】也正常,但是浏览器访问失败,则需要按以下步骤测试。
1、关闭防火墙
systemctl stop firewalld.service
再用浏览器访问,如果成功,说明是 80 端口被拦截
2、安全起见,先重新打开防火墙
systemctl start firewalld.service
3、再单独开放80端口权限,输入以下3行命令
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --reload
firewall-cmd --zone=public --query-port=80/tcp
如果还是失败,就去问问度娘吧。
分别将端口设为8081、 8082、8083
分别将快捷启动命令设为
service tomcat-8081 start
service tomcat-8082 start
service tomcat-8083 start
浏览器 输入 www.mytest.com:8081
即http://192.168.31.53:8081
1、输入命令
vim /usr/local/nginx/conf/nginx.conf
2、修改配置为:
A.http配置方式
#user nobody;
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#设置允许发布内容为500M
client_max_body_size 500M;
client_body_buffer_size 128k;
sendfile on;
keepalive_timeout 65;
#gzip on;
#这个是Nginx转发目标服务器地址
upstream tomcat_server{
server 192.168.31.53:8081;
server 192.168.31.53:8082;
server 192.168.31.53:8083;
}
server {
#监听80端口
listen 80;
#需要拦截的域名或地址,可配置多个
server_name www.mytest.com 192.168.31.53;
#server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/tomcat-8083/;
index index.htm index.html index.jsp;
#Proxy Settings
#拦截后,请求转发规则
proxy_pass http://tomcat_server;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
#root /usr/local/tomcat-8081/webapps/project-name/WEB-INF/static;
#expires 1d;
#}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
B.https配置方式
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#设置允许发布内容为500M
client_max_body_size 500M;
client_body_buffer_size 128k;
sendfile on;
keepalive_timeout 65;
#gzip on;
upstream tomcat_server{
server 127.0.0.1:8081;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}
server {
listen 80;
server_name www.mytest.shop;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name www.mytest.shop;
ssl on;
ssl_certificate /home/webroot/nginxkeystore/mytest.pem;
ssl_certificate_key /home/webroot/nginxkeystore/mytest.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
}
3、运行以下命令,查看配置文件中是否缺少 “{”或 “}”等等
grep -Ei "\{|\}" /usr/local/nginx/conf/nginx.conf
4、进入【/usr/local/nginx/sbin】目录,重新加载Nginx配置
./nginx -s reload
浏览器输入 www.mytest.com
也就是【nginx.conf】配置文件中这段代码的配置
1、默认方式:轮询
upstream tomcat_server{
server 192.168.31.53:8081;
server 192.168.31.53:8082;
server 192.168.31.53:8083;
}
2、weight(权重)
upstream tomcat_server{
server 192.168.31.53:8081 weight=5;
server 192.168.31.53:8082 weight=15;
server 192.168.31.53:8083 weight=30;
}
相当于,50次请求【以下指平均值】
5次是访问8081
15次访问8082
30次访问8083
3、还有其他很多方式配置负载均衡。。。具体问度娘
你应该注意到我上面的配置有这样一段代码
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /usr/local/tomcat-8081/webapps/project-name/WEB-INF/static;
expires 1d;
}
待更新。。。先睡了