使用 IP 地址连接时,如何为本地主机创建 nginx 反向代理?

How can I make nginx reverse proxy for localhost when connectd with IP address?

我在 nginx 上做了这样的反向代理

server {
    listen       80;
    server_name  localhost;

    return 301 https://[my domein]$request_uri;

}

当我访问 http://xxx.xxx.xxx.xxx/index.html 时,这很好用。 我的 nginx 重定向到 https://[我的域]/index.html

但是,当我访问 https://xxx.xxx.xxx.xxx/index.html Chrome 时显示“您的连接不是私人的”错误。 自签名证书无助于避免此错误。需要 CA 签名的证书。 在这种情况下,如何获取本地主机的 SSL 证书?它是 localhost。我认为没有人可以颁发本地主机证书。

有谁知道解决这个问题的好方法吗?

使用 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