markdown Docker Nginx反向代理+密码认证+ http

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Docker Nginx反向代理+密码认证+ http相关的知识,希望对你有一定的参考价值。

# Docker Nginx

先编写nginx配置文件

```
cat nginx_conf/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;

	server {
		listen 8088;
		server_name hsserver.tk;

		location / {
			proxy_pass http://127.0.0.1:8888;
			auth_basic "Please Login";
			auth_basic_user_file /hsli.password;
		}
	}

#    include /etc/nginx/conf.d/*.conf;
}
```

安装`apache2-utils`,使用里面的htpasswd生成密码文件

```
sudo htpasswd -c ~/nginx_conf/hsli.password hsli
```

启动docker,注意配置网络模式,挂载nginx.conf和密码文件

```
docker run --rm --name nginx -d --net=host -v /home/hsli/nginx_conf/nginx.conf:/etc/nginx/nginx.conf -v /home/hsli/nginx_conf/hsli.password:/hsli.password nginx
```

# 配置https 自签ssl证书

https://www.jianshu.com/p/5f9bd492f186

通过openssl生成证书

设置server.key,这里需要设置两遍密码:

```
openssl genrsa -des3 -out server.key 1024 
```

参数设置,首先这里需要输入之前设置的密码:

```
openssl req -new -key server.key -out server.csr
```

然后需要输入如下的信息,大概填一下就可以了,反正是测试用的


```
Country Name (2 letter code) [AU]: 国家名称
State or Province Name (full name) [Some-State]: 省
Locality Name (eg, city) []: 城市
Organization Name (eg, company) [Internet Widgits Pty Ltd]: 公司名
Organizational Unit Name (eg, section) []: 
Common Name (e.g. server FQDN or YOUR name) []: 网站域名
Email Address []: 邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []: 这里要求输入密码
An optional company name []:
```

写RSA秘钥(这里也要求输入之前设置的密码):

```
openssl rsa -in server.key -out server_nopwd.key
```

获取私钥:

```
openssl x509 -req -days 365 -in server.csr -signkey server_nopwd.key -out server.crt
```

完成这一步之后就得到了我们需要的证书文件和私钥了

```
server.crt
server.key
```

`server_nopwd.key`这是免密认真的私钥

## 配置Nginx支持https

配置文件站点下增加ssl相关支持

```
server {
    listen    80;       #侦听80端口,如果强制所有的访问都必须是HTTPs的,这行需要注销掉
    listen    8088 ssl;
    server_name  www.buagengen.com;             #域名

    # 增加ssl
    #ssl on;        #如果强制HTTPs访问,这行要打开
    ssl_certificate /ssl/server.crt;
    ssl_certificate_key /ssl/server_nopwd.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

     # 指定密码为openssl支持的格式
     ssl_protocols  SSLv2 SSLv3 TLSv1.2;

     ssl_ciphers  HIGH:!aNULL:!MD5;  # 密码加密方式
     ssl_prefer_server_ciphers  on;   # 依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

     # 定义首页索引目录和名称
     location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
     }

    #重定向错误页面到 /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
```

然后docker启动nginx时再加上`-v /home/user/nginx_conf/cert:/ssl`
# Let's Encrypt 证书 + docker nginx + 域名认证

## 首先安装certbot

```
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
```

## 然后申请证书,通过域名DNS认证

运行`sudo certbot --manual --preferred-challenges dns certonly`命令,输入域名并同意记录本机IP后开始获取证书,接着certbot就会弹出如下的提示:

```
-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:

IMdfdsfsJDqBRyRaaEgPPQlEuvtxJQAgWZTIVbLuzDi8U

Once this is deployed,
-------------------------------------------------------------------------------
Press Enter to Continue
```

此时certbot程序就会暂停,等待我们去添加DNS记录。

添加DNS的TXT记录
看到上述的提示后,修改域名的DNS记录,添加一条TXT记录,主机名为_acme-challenge,而其中的内容就是letsencrypt生成的随机字符串IMdfdsfsJDqBRyRaaEgPPQlEuvtxJQAgWZTIVbLuzDi8U。

__注意记录主机名为\_acme-challenge.service.hsli.top,要带上\_acme-challenge并加在二级域名之上__

验证成功
添加好DNS记录后,我们可以通过dig -t txt _acme-challenge.service.hsli.top来查看域名的内容,域名生效以后,在certbot程序中下按下回车键,程序继续运行。letsencrypt对DNS记录验证成功,证书就申请成功了。

## 配置nginx

参考

```
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;

	server {
		listen 8088 ssl;
		server_name service.hsli.top;

		# ssl_certificate /ssl/server.crt;
		# ssl_certificate_key /ssl/server_nopwd.key;
		
		ssl_certificate /etc/letsencrypt/live/service.hsli.top/fullchain.pem;
		ssl_certificate_key /etc/letsencrypt/live/service.hsli.top/privkey.pem;
		ssl_trusted_certificate /etc/letsencrypt/live/service.hsli.top/chain.pem;

		# ssl_session_cache	shared:SSL:1m;
		# ssl_session_timeout	5m;
		
		# ssl_protocols	SSLv2 SSLv3 TLSv1.2;

		# ssl_ciphers HIGH:!aNULL:!MD5;
		# ssl_prefer_server_ciphers	on;

		location / {
			proxy_pass http://127.0.0.1:8888;
			auth_basic "Please Login";
			auth_basic_user_file /hsli.password;
		}
	}
	server {
		listen 7500 ssl;
		server_name service.hsli.top;

		ssl_certificate /etc/letsencrypt/live/service.hsli.top/fullchain.pem;
		ssl_certificate_key /etc/letsencrypt/live/service.hsli.top/privkey.pem;
		ssl_trusted_certificate /etc/letsencrypt/live/service.hsli.top/chain.pem;

		location / {
			proxy_pass http://127.0.0.1:3000;
		}
	}


#    include /etc/nginx/conf.d/*.conf;
}
```

指定了密钥位置

## 启动nginx docker

多加一个目录映射

```
docker run --rm --name nginx --net=host -v /home/hsli/nginx_conf/nginx.conf:/etc/nginx/nginx.conf -v /home/hsli/nginx_conf/hsli.password:/hsli.password -v /etc/letsencrypt:/etc/letsencrypt -d nginx
```

## 自动更新证书

证书默认有效期只有90天,加crontab任务自动更新

自动更新命令:

```
sudo certbot certonly --renew-by-default -d YOURDOMAIN --manual --preferred-challenges dns
```

加到crontab里面

```
0 12 25 * * certbot certonly --renew-by-default -d service.hsli.top --manual --preferred-challenges dns
```

每月25号12点0分自动更新证书

## 更新完证书需要重启nginx?

# 非标准端口强制https

站点配置加上

```
error_page 497 https://$host:port$request_uri;
```

```
server {
  listen      1234 ssl;
  server_name your.site.tld;
  ssl         on;
  ...
  error_page  497 https://$host:1234$request_uri;
  ...
  location
  ...
}
```

以上是关于markdown Docker Nginx反向代理+密码认证+ http的主要内容,如果未能解决你的问题,请参考以下文章

为Docker创建自动化nginx反向代理

Docker 安装 Nexus3,并配置 Nginx 反向代理

markdown 正向代理和反向代理的区别[proxy,nginx]

docker 安装 nginx 并配置反向代理

docker中的nginx反向代理

docker nginx 反向代理