1 申请免费SSL证书
访问 https://certbot.eff.org
,可申请 通配符证书
,每次申请可以使用3个月,到期可以免费续期。
2 配置nginx server
index.conf 配置如下:
server {listen 80;server_name biwow.com www.biwow.com;return 301 https://$server_name$request_uri;
}server {listen 443 ssl http2 default_server;server_name biwow.com www.biwow.com;ssl_certificate cert/biwow.com.pem;ssl_certificate_key cert/biwow.com.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://10.0.24.11:8000;}
}# 处理个性化域名
server {listen 443 ssl http2;server_name ~^(?<subdomain>[a-z0-9]+)\.biwow.com$; # 匹配个性化子域名ssl_certificate cert/biwow.com.pem;ssl_certificate_key cert/biwow.com.key;location / {# 根据子域名获取相关配置,并进行相应的代理转发proxy_pass http://10.0.24.11:8100/$subdomain; # 假设根据子域名确定后端服务}
}
8000.conf 配置如下
server {listen 8000;charset utf-8;location / {root /usr/share/nginx/html/8000/;try_files $uri $uri/ /index.html;}location /api {add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';if ($request_method = 'OPTIONS') {return 204;}proxy_pass http://10.0.24.11:18000;proxy_set_header Host $proxy_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
8100.conf 配置如下:
server {listen 8100;charset utf-8;location / {root /usr/share/nginx/html/8100/;try_files $uri $uri/ $request_uri.html;}location /api {add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';if ($request_method = 'OPTIONS') {return 204;}proxy_pass http://10.0.24.11:18100;proxy_set_header Host $proxy_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}
3 启动nginx的脚本
nginx.sh 配置如下:
#!/bin/bashdocker rm -f nginxdocker run -itd --restart=unless-stopped \-v /etc/localtime:/etc/localtime \-v /etc/timezone:/etc/timezone \--network=host \--name nginx \-v /opt/docker/nginx/html:/usr/share/nginx/html \-v /opt/docker/nginx/cert:/etc/nginx/cert \-v /opt/docker/nginx/logs:/var/log/nginx \-v /opt/docker/nginx/nginx.conf:/etc/nginx/nginx.conf \-v /opt/docker/nginx/conf:/etc/nginx/conf.d \nginx:alpine docker logs -f nginx