一. 问题背景
用docker方式把nginx部署在阿里云上面,在阿里云上面启动自己的应用程序(端口8083)。想要实现的效果是用nginx做反向代理,本地电脑发送请求,nginx收到请求后转发到8083的应用程序。但是一直无法正确访问nginx,做不出效果。
二. 前言
后台应用是基于SpringBoot 2.x构建的。docker的nginx容器是latest版本。 笔者目前使用的解决方案是重新创建一个nginx容器,步骤略有些麻烦。
三. 解决方案
3.1 储备知识
首先需要了解一下nginx容器的配置文件的逻辑:
nginx容器的配置文件的逻辑关系是这样的:nginx容器里面的配置文件是在
/etc/nginx/
,里面有一个nginx.conf
文件和一个conf.d
文件夹。nginx.conf
是主配置文件;conf.d
存放的是子配置文件(默认是有一个default.conf
文件),可以有若干个子配置文件。其实主配置文件是配置http块的,子配置文件就是配置server块的。官方推荐用子配置文件做配置
nginx.conf文件,如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
# 子配置文件的位置
include /etc/nginx/conf.d/*.conf;
}
注意:上面的代码块中很重要的是最后一行
include /etc/nginx/conf.d/*.conf;
,它的作用就是指定子配置文件的位置,能让nginx找到。也能发现nginx.conf
中确实没有server块
default.conf文件,如下:
server {
listen 80;
listen [::]:80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
# proxy_pass http://localhost:xxx;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
注意:可以看到
default.conf
就是配置server块的。需要注意listen监听的端口值就是在创建容器命令里面指定的。注意要写ip地址的地方不要用localhost,否则会有问题,用127.0.0.1或者局域网的ip地址代替localhost。
3.2 创建nginx容器
下载nginx镜像:
docker pull nginx
创建nginx容器
docker run -p 11180:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-d nginx
复制nginx容器的配置文件到阿里云:
docker container cp nginx:/etc/nginx /mydata/nginx/
将刚才复制过来的文件夹重命名:
mv nginx conf
删除刚才创建的nginx容器(因为要重新挂载配置文件):
docker run -p 11180:80 --name nginx \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-v /mydata/nginx/conf:/etc/nginx \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-d nginx
解释:上面代码块的第4个
-v
是挂载nginx容器的子配置文件。在前面的储备知识有提到。
3.3 配置
创建容器的命令指定的端口号是阿里云用11180,nginx容器用80。因此
conf.d/default.conf
中监听的端口是80,而不是11180。在浏览器发送请求访问的应该是11180。应用程序的端口是8083,proxy_pass应该配置8083。
default.conf,如下:
server {
listen 80;
listen [::]:80;
server_name xx.xx.xx.xx;
location / {
proxy_pass http://xx.xx.xx.xx:8083;
}
}
3.4 测试
浏览器发送请求:
http://xx.xx.xx.xx:11180/xxx/xxxx;
解释:
/xxx/xxxx
为后端controller的映射路径