【问题标题】:How can I make nginx reverse proxy for localhost when connectd with IP address?与 IP 地址连接时,如何为 localhost 制作 nginx 反向代理?
【发布时间】:2021-10-21 23:27:59
【问题描述】:
我像这样在我的 nginx 上做了反向代理
server {
listen 80;
server_name localhost;
return 301 https://[my domein]$request_uri;
}
这很好用,当我访问 http://xxx.xxx.xxx.xxx/index.html 时。
我的 nginx 重定向到 https://[my domain]/index.html
但是,当我访问 https://xxx.xxx.xxx.xxx/index.html 时,Chrome 显示“您的连接不是私密的”错误。
自签名证书无助于避免此错误。需要 CA 签名的证书。
在这种情况下,如何获取 localhost 的 SSL 证书?它是 localhost。我认为没有人可以颁发 localhost 证书。
有人知道解决这个问题的好方法吗?
【问题讨论】:
标签:
nginx
ssl
localhost
nginx-reverse-proxy
【解决方案1】:
使用 mkcert。
安装 mkcert
sudo apt install libnss3-tools
检查 latest version 的 mkcert 发布页面。在撰写本文时,最新版本是.v1.4.3
export VER="v1.4.3"
wget -O mkcert https://github.com/FiloSottile/mkcert/releases/download/${VER}/mkcert-${VER}-linux-amd64
chmod +x mkcert
sudo mv mkcert /usr/local/bin
安装证书
生成本地受信任的 SSL 证书
mkcert -install
ls -1 ~/.local/share/mkcert
mkdir ~/cert && cd ~/cert
mkcert crm.site '*.crm.site' localhost 127.0.0.1 ::1
添加到 nginx
sudo nano /etc/nginx/sites-available/crm.site
server {
listen *:443 ssl http2;
index index.php;
root /home/andrey/crm.site;
server_name crm.site *.crm.site;
ssl_certificate /home/andrey/cert/crm.site+4.pem;
ssl_certificate_key /home/andrey/cert/crm.site+4-key.pem;
client_max_body_size 128M;
client_body_buffer_size 128k;
location / {
try_files $uri $uri/ /index.php?$args;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains" always;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
然后重启 nginx
sudo service nginx restart