docker 使用 nginx 并配置域名
1、首先去申请域名,拿到ssl证书,在挂在出来的nginx路径下创建一个cert文件夹,复制进去。1、docker启动容器后,在挂载出来的nginx里找到nginx.conf。,输入http://ip,使用项目访问,各种项目皆访问成功,即配置成功。可以直接访问查看 http://ip,出现这个就是启动成功。
目录
1、docker启动容器后,在挂载出来的nginx里找到nginx.conf
5、登入项目测试内部转发是否成功,注:各个容器都有不同的ip,不能用127.0.0.1
1、首先去申请域名,拿到ssl证书,在挂在出来的nginx路径下创建一个cert文件夹,复制进去
一、docker 安装nginx
1、搜索nginx 镜像
docker search nginx
2、拉取nginx镜像
docker pull nginx:latest
3、运行镜像
docker run -d --name nginx -p 80:80 -p 443:443 nginx:latest
4、拷贝 nginx容器中的文件到主机目录
docker cp nginx:/etc/nginx/nginx.conf /usr/local/nginx/
docker cp nginx:/etc/nginx/conf.d /usr/local/nginx/conf/
docker cp nginx:/usr/share/nginx/html /usr/local/nginx/html
docker cp nginx:/var/log/nginx/ /usr/local/nginx/logs/
5、停止并删除容器
docker stop nginx
docker rm nginx
6、重启nginx
docker run -d --name nginx -p 80:80 -p 443:443 -v /usr/local/nginx/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/logs:/var/log/nginx -v /usr/local/nginx/html:/usr/share/nginx/html -v /usr/local/nginx/conf:/etc/nginx/conf.d --privileged=true -e TZ=Asia/Shanghai nginx:latest
7、检测nginx是否启动成功
可以查看log
docker logs nginx
可以直接访问查看 http://ip,出现这个就是启动成功
二、nginx配置转发多个端口
1、docker启动容器后,在挂载出来的nginx里找到nginx.conf
2、或者进入容器,用指令修改配置文件
此时如果报错
如果是基于Debian或Ubuntu的系统,可以使用
sudo apt-get install vim
。如果是基于Red Hat的系统,可以使用
sudo yum install vim
或sudo dnf install vim
如果是macOS,可以使用
brew install vim
编辑完 按esc 输入指令:wq 即可
3、配置文件
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
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;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/html;
index login.html index.htm;
}
# 实例一的前端页面,主要root 要换成alias
location /web1 {
# 多个前端项目,需要使用alias
alias /usr/local/nginx/html/web1 ;
index login.html index.htm;
}
# 实例二的前端页面
location /web2 {
alias /usr/local/nginx/html/web2;
index login.html index.htm;
}
# 实例一的后端配置,主要看proxy_pass
location /java1/ {proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
proxy_pass http://ip:port1/;
}
location /java2/ {proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
proxy_pass http://ip:port2/;
}
}
}
4、配置后重启nginx
docker restart nginx
5、登入项目测试内部转发是否成功,注:各个容器都有不同的ip,不能用127.0.0.1
三、配置域名
1、首先去申请域名,拿到ssl证书,在挂在出来的nginx路径下创建一个cert文件夹,复制进去
2、将证书复制到容器里
docker cp /usr/local/nginx/cert nginx:/etc/nginx/
3、修改配置文件
user nginx;
worker_processes auto;error_log /var/log/nginx/error.log notice;
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;
server {
listen 80;
server_name xxx.xxx.com; # 你的域名
return 301 https://$server_name$request_uri; # 重定向到HTTPS
}
server {
listen 443 ssl;
server_name xxx.xxx.com;ssl_certificate /etc/nginx/cert/xxx.xxx.com.pem; # SSL证书文件路径
ssl_certificate_key /etc/nginx/cert/xxx.xxx.com.key; # SSL私钥文件路径
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /web1 {
alias /usr/share/nginx/html/web1 ;
index index.html;
}
location /web2 {
alias /usr/share/nginx/html/web2 ;
index index.html;
}
location /java1/ {proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;proxy_pass http://xxx.xxx.com:port1;
}
location /java2/ {proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;proxy_pass http://xxx.xxx.com:port2;
}
location /java3/ {proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;proxy_pass http://xxx.xxx.com:port3;
}}
}
4、重启nginx
docker restart nginx
5、检测是否配置成功
在浏览器输入域名或https://xxx.xxx.com/,输入http://ip,使用项目访问,各种项目皆访问成功,即配置成功
参考链接:docker 安装 nginx 发布Vue项目_docker nginx vue-CSDN博客
更多推荐
所有评论(0)