安装nginx + ip_hash配置集群
参考网址:https://www.cnblogs.com/kaid/p/7640723.html
查询历史记录
history|grep run
创建容器
docker run -d --name m-nginx -m 4G --privileged=true --link m-mysql:mydocker-mysql -p 9006:22 -p 9086:80 -p 9007:8080 -v /d_nginx:/usr/local/d_nginx centos-ssh-root /usr/sbin/init
docker ps -a
docker rm -f aa
docker-enter aa
yun install -y wget
查询nginx版本:选择稳定版
cd /usr/local/d_nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
ll
rm -rf nginx-1.14.0.tar.gz
ll
mv nginx-1.14.0.tar.gz.1 nginx-1.14.0.tar.gz
ll
tar -zxvf nginx-1.14.0.tar.gz
ll
mv nginx-1.14.0 /usr/local/nginx
cd /usr/local/nginx
ll
安装gcc环境
yum install gcc-c++
y
安装perl库
yum install-y pcre pcre-devel
安装zlib库
yum install -y zlib zlib-devel
安装openssl库
yum install -y openssl openssl-devel
pwd
cd ../
ll
rm -rf nginx/
cd
cd /usr/local/d_nginx/
ll
tar -zxvf nginx
ll
./configure
make
make install
或者:make && make install
whereis nginx
cd /usr/local/nginx/sbin
./nginx
netstat -tnlup
yum install -y net-tools
netstat -tnlup
nginx+部署项目:
打包项目:package,再target中生成jar包
上传到root中:
进入nginx容器sbin目录下编辑nginx.conf
cd ../
ll
cd conf/
ll
cp nginx.conf nginx.conf.2018-08-20.bak
vi nginx.conf
编辑信息如下:设置一个conf的包含文件来分层展示
#gzip on;
下加这一句代替下一段
inclede /usr/local/nginx/myconf/*.conf;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.html;
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50.html {
root html;
}
}
esc>>:wq
当前目录应该是:nginx的conf目录:
cd ../
mkdir myconf
cd myconf/
ll
pwd
vi www.1601x.com.conf
复制:资源文件:https://blog.csdn.net/jackliu16/article/details/79444327
# 设定负载均衡后台服务器列表
upstream 1601xSpringboot {
#ip_hash;
server 127.0.0.1:8080 max_fails=2 fail_timeout=30s ;
server 127.0.0.1:8081 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name www.1601x.com;
root /usr/local/myproject/www;
charset utf-8;
#access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /usr/local/myproject/www;
index index.jsp index.html index.htm;
proxy_pass http://1601xSpringboot;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
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;
}
#静态文件,nginx自己处理,不去1601xSpringboot请求tomcat
location ~* /download/ {
root /usr/local/myproject/fs;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /usr/local/myproject/www;
expires 7d;
}
#location /nginx_status {
# stub_status on;
# access_log off;
# allow 192.168.10.0/24;
# deny all;
#}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
}
}
## 其它虚拟主机,server 指令开始
:wq 保存(注释去除以防报错)
cd ../
ll
进入到nginx层中
./sbin/nginx -s stop
vi /usr/local/nginx/conf/nginx.conf 可能报错:修改格式
vi /usr/local/nginx/myconf/www.1601x.conf 查看这两个文件需要注意
显示文本行号:
set +空格
./sbin/nginx -t 测试
ps -aux 查看进程(Address already in use)
再在nginx目录下执行运行:./sbin/nginx
访问: cd myconf/
cat www.1601x.com.conf
查看:当前请求为server_name 监听为80
需要配置本地域名解析:
电脑>>windows>>system32>>drivers>>etc>>hosts
编辑: 39.107.111.7(服务器ip) www.1601x.com
cmd窗口测试:ping www.1601x.com
浏览器访问:www.1601x.com:+(映射出来的端口:8080) 9086
cd ../
到nginx位置
查看访问日志指向的位置(来用作记日志)
cat /conf/nginx.conf
cat myconf/www.1601x.com.conf
vi myconf/www.1601x.com.conf
再charset utf-8;
下添加:
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"http_user_agent" "$http_x_forwarded_for" '
# '$upstream_addr $upstream_response_time $request_time ;
access_log logs/host.access.log main;
停止:
ps -aux
kill 2739
ll
set nu 空格
重启./sbin/nginx>>>>>>>>>>>>>>>>>>>不支持上述配置
vi conf/nginx.conf
放开:http中的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;
./sbin/nginx -s reload
./sbin/nginx
查看日志: tail -100f access.log /tail -f *.log
通过日志信息来查找错误:
nginx会轮询操作来查看端口来测试.
java -jae demo-0.0.1-SNAP
需要创建三个容器或更多来支持
docker run -d --name m-springboot1 -m 4G --privileged=true ---link m-nginx:nginx1 -p 9006:22 -p 9088:8080 -v /d_springboot1:/usr/local/d_springboot1 centos-ssh-root /usr/sbin/init
cp demo-0.0.1-SNAPSHOT.jar /d_springboot1
cp jdk-8u181-linux-x64.rpm /d_springboot1
docker-enter aa
cd /usr/local/d_springboot1/
ll
rpm -ivh jdk-8u181-linux-x64.rpm
java -version
java -jar demo-0.0.1-SNAPSHOT.jar
启动以后会有地址如:/api/goods/findSolrData 等等访问的路径
浏览器访问www.1601x.com:9086//api/goods/findSolrData
报错查看日志,502服务端错误,,连接失败,轮询连接均失败,需要操作:
查看docker的ip
可以自己设置让服务器空闲发送请求:再会话选项,终端反空闲:选择发送协议NO-OP(服务器内置设置来节省资源,定时发送心跳来连接,测试不让服务断掉)
docker ps
docker inspect
过滤得到应用的ngnix的ip
docker inspect m-springboot1|grep IPAddress
知道ip地址可以再ngnix中修改:
--link会做一个本地域名解析
docker ps
docker-enter myspringboot1
cat etc/host
cat /etc/host
可以查看到连接的ip
vi /etc/host 添加本地域名解析
在后面添加:
172.18.0.7 springboot1 容器id 容器名称
172.18.0.8 springboot2 容器id 容器名称
:wq
修改nginx配置
nginx层:
cd ../
ll
vi ./myconf/www/1601x.com.conf
springboot1:8080
springboot2:8080
端口
docker run -d --name m-springboot1 -m 4G --privileged=true ---link m-nginx:nginx1 -p 9007:22 -p 9088:8080 -v /d_springboot1:/usr/local/d_springboot1 centos-ssh-root /usr/sbin/init
exit
tar -zx
vi ./myconf/www.1601x.com
nginx 配置本地域名解析来连接容器
重新载入
./sbin/nginx -s reload
清理日志
> access.log
tail -f *.log
参数指定:?name="手机"
docker ps
查看实时日志
docker logs -f -t 容器名