目录
1. nginx前置操作
2. 自定义容器
3. nginx常用命令
4. Error
4.1 502(无响应网关/代理)
4.2 404(找不到对应页面)
4.3 400(异常请求)
4.4 响应超时问题
5. 完整版本Nginx配置文件
1. nginx前置操作
1. 下载拉取nginx镜像
docker pull nginx
2. 使用nginx镜像创建容器 X X X (不建议使用)
docker run -itd --name nginx -p 8080:80 nginx:latest
3. 无法使用vim指令,所以需要进行目录挂载
docker run -itd \
--name nginx01 \
-v /home/nginx/conf.d:/etc/nginx/conf.d \
-v /home/nginx/html:/etc/nginx/html \
-v /home/nginx/log:/usr/log/nginx \
-p 8080:80 \
nginx
第一个-v:挂载nginx的主配置文件,以方便在宿主机上直接修改容器的配置文件
第二个-v:挂载静态页面目录
第三个-v:挂载容器内nginx的日志,容器运行起来之后,可以直接在宿主机的这个目录中查看 nginx日志
4. 进入容器内
docker exec -it nginx01 bash
2. 自定义容器
docker run -it --name spring01 --net mynet -p 8081:8080 spring:2.0
3. nginx常用命令
nginx -t # 检查配置文件
nginx -s reload # 重读配置文件
service nginx start # 启动
service nginx stop # 停止
service nginx restart # 重新启动
service nginx reload # 重新加载配置,平滑启动
service nginx status # 查看状态
4. Error
4.1 502(无响应网关/代理)
upstream tomcatList {
server 172.18.0.5:8080 weight=1;
server 172.18.0.3:8080 weight=2;
server 172.18.0.4:8080 weight=2;
}
- 端口或者协议不一致
4.2 404(找不到对应页面)
location ~^/api/ {
proxy_pass http://tomcatList;
proxy_redirect default;
}
- 路径映射存在问题
- proxy_pass后面的路径以/结尾
- #rewrite ^/api/(.*)$ /$1 break; 重写规则
4.3 400(异常请求)
java.lang.IllegalArgumentException: The character [_] is never valid in a domain
name.
at
org.apache.tomcat.util.http.parser.HttpParser$DomainParseState.next(HttpParser.j
ava:974) ~[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.tomcat.util.http.parser.HttpParser.readHostDomainName(HttpParser.java
:870) ~[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at org.apache.tomcat.util.http.parser.Host.parse(Host.java:71) ~[tomcatembed-core-9.0.41.jar!/:9.0.41]
at org.apache.tomcat.util.http.parser.Host.parse(Host.java:45) ~[tomcatembed-core-9.0.41.jar!/:9.0.41]
at
org.apache.coyote.AbstractProcessor.parseHost(AbstractProcessor.java:298) ~
[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.coyote.http11.Http11Processor.prepareRequest(Http11Processor.java:797
) [tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:350)
[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.ja
va:888) [tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:15
97) [tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
[tomcat-embed-core-9.0.41.jar!/:9.0.41]
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[na:1.8.0_221]
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[na:1.8.0_221]
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:6
1) [tomcat-embed-core-9.0.41.jar!/:9.0.41]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]
- springboot无法识别负载均衡中的_符号
upstream tomcatList {
server 172.18.0.5:8081 weight=1;
server 172.18.0.3:8081 weight=2;
server 172.18.0.4:8081 weight=2;
}
4.4 响应超时问题
- 检查网段是否互通
docker network connect mynet nginx
5. 完整版本Nginx配置文件
#服务器的集群
upstream tomcatList { #服务器集群名字
server 172.18.0.5:8081 weight=1; #服务器1 weight是权重的意思,权重越大,分配的概率越大
server 172.18.0.3:8081 weight=2; #服务器2 weight是权重的意思,权重越大,分配的概率越大
server 172.18.0.4:8081 weight=2; #服务器3 weight是权重的意思,权重越大,分配的概率越大
}
server {
listen 80;
server_name www.zking.com;
location / {
root /etc/nginx/html/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~^/api/ {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://tomcatList;
proxy_redirect default;
}
}