dcoker 安装配置nginx

本文介绍了如何使用Docker安装Nginx,启动容器并挂载目录,然后配置Nginx转发HTTP和HTTPS请求。通过修改nginx.conf文件,设置代理转发到指定服务器,并添加SSL证书实现HTTPS支持。最后,重启Nginx容器使配置生效。
摘要由CSDN通过智能技术生成

安装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 服务器证书安装

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值