Nginx配置https和域名转发
<p>在我的Docker内网中,有一个服务:http://172.18.0.3:9000,我如何才能在公网上通过域名访问到这个服务呢?Nginx可以帮我做。</p><p>直接贴配置:</p>
<pre><code class="language-nginx">server {
listen80;
server_name xxx.xxxx.com;
rewrite ^(.*)$https://$host$1 permanent;
}
server {
listen443 ssl;
server_name xxx.xxxx.com;
ssl_certificate /etc/nginx/conf.d/cert/portainer.goodboytxb.com/cert1.pem;
ssl_certificate_key/etc/nginx/conf.d/cert/portainer.goodboytxb.com/privkey1.pem;
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://172.18.0.3:9000;
}
}
</code></pre>
<ol>
<li>
<p>server_name xxx.xxxx.com;:是监听的解析到当前服务器的域名,一般单独的一个服务用一个子域名去映射,这里需要在域名的DNS管理中添加一个xxx的子域名解析的A记录。</p>
</li>
<li>
<p>rewrite ^(.*)$https://$host$1 permanent; :这一句是80端口下的请求统一重写为https,即将http的请求强制转换成https</p>
</li>
<li>
<pre><code class="language-nginx">ssl_certificate /etc/nginx/conf.d/cert/portainer.goodboytxb.com/cert1.pem;
ssl_certificate_key/etc/nginx/conf.d/cert/portainer.goodboytxb.com/privkey1.pem;
# 这两句的是指定https的证书和私钥的位置,https必须需要证书
</code></pre>
</li>
<li>
<pre><code class="language-nginx">location / {
proxy_pass http://172.18.0.3:9000;
}
#通过443端口的流量统一转发到 http://172.18.0.3:9000;
</code></pre>
</li>
</ol>
<p>此时通过 xxx.xxxx.com 就可以对应的访问到 http://172.18.0.3:9000</p>
<p>配置文件 xxx.xxxx.com.conf 需要放到nginx的conf.d文件夹下,conf.d是Nginx的默认配置文件夹,这个目录是在Nginx.conf中配置的。</p>
<p>http://172.18.0.3:9000是docker中的内网ip,这里也可以用容器名的方式访问,比如容器名是portainer,配置成http://portainer:9000也是可以访问到的。</p><br><br>
来源:https://www.cnblogs.com/txbblog/p/16279759.html
頁:
[1]