在docker上发布netcore 遇到的坑:
Nginx
1.准备工作:
创建nginx的挂载目录:
[root@xx/] mkdir -p ~/my/nginx/www ~/my/nginx/logs ~/my/nginx/conf.d ~/my/nginx/cert拷贝容器内 Nginx 默认配置文件到本地当前目录下的nginx 目录
[root@xx/] docker cp nginx容器ID:/etc/nginx/nginx.conf ~/mynginx
说明:
- www: 目录将映射为 nginx 容器配置的虚拟目录。
- logs: 目录将映射为 nginx 容器的日志目录。
- conf: 目录里的配置文件将映射为 nginx 容器的配置文件。
- cert: 目录里的配置文件将映射为 nginx 容器的ssl证书。
docker run --name nginx -p 443:443 -p 80:80 -v /my/nginx/www:/usr/share/nginx/html -v /my/nginx/nginx.conf:/etc/nginx/nginx.conf -v /my/nginx/conf.d:/etc/nginx/conf.d -v /my/nginx/logs:/var/log/nginx -v /my/nginx/cert:/etc/nginx/cert -d nginx
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;
}
站点conf配置(放在宿主机/etc/nginx/conf.d目录)
upstream lol580
{
server 172.17.0.1:5000 max_fails=5 fail_timeout=60s;
}
server {
listen 80;
server_name www.lol580.com;
access_log /var/log/nginx/lol580.access.log main;
error_log /var/log/nginx/lol580.error.log error;
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.17.0.1:5000/;
}
}
坑:
1.connect() failed (111
connect() failed (111: Connection refused) while connecting to upstream
> 解决方案:为nginx在docker中,所以不能使用127.0.0.1:5000来访问宿主机里的应用,docker内部实际上实现了一个虚拟网桥docker0,所以要通过宿主机内网地址(172.17.0.1)来访问.
>通过ifconfig命令 查看docker0的内网ip为172.17.0.1
2.nginx connect() failed 113
nginx connect() failed (113: No route to host) while connecting to upstream
>解决方案:
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
3.upstream sent unsupported FastCGI protocol
[error] 6#6: *1 upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream, client: 183.69.235.66, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:80", host: "doc.lol580.com"
解决方案:
配置文件如下:
server {
listen 443 ssl;
server_name www.xx.com;
charset utf-8;
ssl_certificate /etc/nginx/cert/wwwxxcom.pem;
ssl_certificate_key /etc/nginx/cert/wwwxxcom.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 / {
proxy_pass http://172.17.0.1:5000;
}
}
server {
listen 80;
server_name www.xx.com; #将localhost修改为您证书绑定的域名,例如:www.example.com。
rewrite ^(.*)$ https://$host$1 permanent; #将所有http请求通过rewrite重定向到https。
location / {
proxy_pass http://172.17.0.1:5000;
}
}