NGINX リバース プロキシが HTTPS を HTTP にリダイレクトする

NGINX リバース プロキシが HTTPS を HTTP にリダイレクトする

私は nginx を使用してリバース プロキシを設定しており、次のように動作させる必要があります。

リバースプロキシ構造

クライアントは私の URL (cron.mocxmonitoramento.com.br) にアクセスし、リバース プロキシに落ちます。これにより、プロキシは必要に応じてサーバー 1 または 2 または 3 に接続を誘導する必要があります。このような構成では、HTTP アクセスを行うときは正しく機能しますが、HTTPS 接続を行うとリダイレクトは正しく機能しません。

サーバー 1、2、3 は Laravel 7 アプリケーションを実行します。すべてのサーバーに nginx がインストールおよび構成されています。

サーバー 1、2、3 の構成は次のとおりです (これらは同一です)。

server {
listen 80 default_server;
listen [::]:80 default_server;

server_name _;

access_log /home/ubuntu/mocxmonitoramento.com.br/logs/access.log;
error_log /home/ubuntu/mocxmonitoramento.com.br/logs/error.log;

root /home/ubuntu/mocxmonitoramento.com.br/public/public/;
index index.php;

location / {
   try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}
}

リバース プロキシの構成は次のとおりです。

upstream cron {
    server 10.0.1.30;
    server 10.0.1.31;
    server 10.0.1.32;
}
server {
    listen 443 ssl;

    server_name cron.mocxmonitoramento.com.br www.cron.mocxmonitoramento.com.br;

    ssl_certificate /etc/letsencrypt/live/cron.mocxmonitoramento.com.br/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cron.mocxmonitoramento.com.br/privkey.pem;

    error_page 502 /502.html;
    location = /502.html {
            root /usr/local/nginx/html;
            internal;
    }

    location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_pass http://cron;
            proxy_redirect default;
            proxy_redirect / $scheme://$http_host/xcharge/;

            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $remote_addr;
    }

    location = /xcharge {
            return 301 $scheme://$http_host$uri/$is_args$args;
    }
}
server {
    listen 80;

    server_name cron.mocxmonitoramento.com.br www.cron.mocxmonitoramento.com.br;

    error_page 502 /502.html;
    location = /502.html {
            root /usr/local/nginx/html;
            internal;
    }

    location / {
            proxy_set_header   X-Forwarded-For $remote_addr;
            proxy_pass http://cron;
            proxy_redirect default;
            proxy_redirect / $scheme://$http_host/xcharge/;

            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-For $remote_addr;
    }

    location = /xcharge {
            return 301 $scheme://$http_host$uri/$is_args$args;
    }
}

ご覧のとおり、サーバー 1、2、3 の nginx 構成ファイルでは、ポート 80 のみをリッスンしており、SSL 証明書はありません。一方、リバース プロキシはポート 80 とポート 443 の両方をリッスンしており、証明書は正常にインストールされています (certbot を使用しています)。

ポート 80 経由のアクセスは正常に機能しますが、https にアクセスすると、ブラウザーに次のエラーが表示されます。

ブラウザ エラー: このリクエストはブロックされました。コンテンツは HTTPS 経由で提供される必要があります。

つまり、ブラウザは HTTPS 接続を確立しましたが、Laravel アプリケーションは安全でない接続 (HTTP) を介してデータを取得しようとしており、ブラウザはその読み込みをブロックしています。この問題の解決策を見つけるためにインターネットで何度か検索しましたが、役立つものは何も見つかりませんでした。誰かがすでにこの方向で何か役立つことをしているかどうか知りたいです。

私のnginxのインストールは標準的なインストールではなく、nginxコードをコンパイルしてインストールすることで行いました。Nginx スティッキーモジュールオープンライブラリ。

次のようにインストールしました:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install libpcre3 libpcre3-dev -y
sudo apt install build-essential checkinstall zlib1g-dev -y

CertBot

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install certbot -y
sudo ufw allow 80
sudo certbot certonly --standalone --preferred-challenges http -d {URL}
sudo certbot renew --dry-run

Github do modulo Styck
https://github.com/Refinitiv/nginx-sticky-module-ng
git clone https://github.com/Refinitiv/nginx-sticky-module-ng.git
wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz
tar -xf openssl-1.1.1k.tar.gz

Instalando nginx com o modulo styck
Acesse e procure a versão do Nginx 1.22.1.
http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.22.1.tar.gz
tar -xvzf nginx-1.22.1.tar.gz
cd nginx-1.22.1
./configure --with-http_ssl_module --add-module=/home/ubuntu/nginx-sticky-module-ng --with-openssl=/home/ubuntu/openssl-1.1.1k
make
sudo make install

その後、OS ubuntu 20.04の/usr/local/nginxにインストールされるため、nginxのファイルとディレクトリの基本的な設定がいくつかあります。

とにかく、同じような経験をした人がいて、私を助けてくれるかどうか知りたいです。皆さん、ありがとうございます。

答え1

おそらく、httpsそこに URL を配置するには、Laravel ルート URL を適切に設定する必要があります。

関連情報