这里写自定义目录标题
安装nginx
1.下载nginx镜像
docker pull nginx:stable
2.启动nginx容器
docker run -d -p 80:80 -p 443:443 --name nginx \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/conf:/etc/nginx \
-v /home/nginx/logs:/var/log/nginx \
nginx:stable
-p :将容器内部使用的网络端口随机映射到我们使用的主机上。80是http,443是https
-v: 把重要的目录挂载到主机上,
这里在主机上新建了www,conf,logs三个目录,分别挂载nginx的html ,配置文件,和日志
配置nginx
由于配置文件都挂载到了home/nginx/conf目录,这里主要修改他
转发请求
home/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;
include /etc/nginx/conf.d/*.conf;
}
include /etc/nginx/conf.d/*.conf;:conf.d目录的所有配置文件都被引用进来,因此我在conf.d 新建一个xxx.conf配置文件来转发xxx.com域名的请求
//xxx.conf
server {
listen 80; //监听nignx80端口
server_name xxx.xxx.com;//监听请求的域名
location / {
proxy_pass http://49.234.xxx.xxx:8080/; //转发目的
}
}
保存文件之后,docker restart nginx 重启容器
然后 在浏览器输入xxx.xxx.com 就被会nginx捕捉到并且转发到http://49.234.xxx.xxx:8080
添加https
server {
listen 80;
listen 443 ssl;
server_name xxx.xxx.com;//监听请求的域名
ssl_certificate xxx.xxx.com_bundle.crt;
ssl_certificate_key xxx.xx.com.key;
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 / {
proxy_pass http://49.234.xxx.xxx:8080/; //转发目的
}
}
注意私钥xxx.xx.com.key 和证书xxx.xxx.com_bundle.crt 放在/home/nginx/conf目录下
重启nginx即可。
参考链接: Nginx 服务器证书安装