nginxリバースプロキシを使用して同じポートからhttpからhttpsにリダイレクトする

nginxリバースプロキシを使用して同じポートからhttpからhttpsにリダイレクトする

http://mydomain.com/myrojectUbuntu 14.04(Apache Web Server)にウェブサイトを構築しました。

同じホストのポート5000で実行されるhtsqlサービスを設定しました。

working links:
http://mydomain.com/myproject
http://mydomain.com:5000/region

その後、このサーバーにSSL証明書をインストールしてWebサイトを実行しました。https

https://mydomain.com/myproject動作しますが、https://mydomain.com:5000/region動作しません。これは、ポート5000がすでに使用されており、そのポートでhtsqlサービスが実行されているためです。

これで問題は、nginxリバースプロキシを使用して同じポート(5000)からhttpからhttpsにリダイレクトする方法です。

つまり、https://mydomain.com:5000/region動作する必要があります

私の考えは、nginxに別のポート(5001など)を設定し、リクエストをhttps、5000ポートに転送することです。

以下はApacheの設定ファイルです。

/etc/apache2/ports.conf

Listen 80

<IfModule ssl_module>
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

/etc/apache2/sites-enabled/default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        ServerName mydomain.com
        SSLEngine on
        SSLCertificateFile /home/ubuntu/project.crt
        SSLCertificateKeyFile /home/ubuntu/project.key
</VirtualHost>

nginx設定ファイル:

基本プロファイル

server {
        listen 5001 ssl;

        server_name my domain.com;
        ssl on;
        ssl_certificate /home/ubuntu/project.crt;
        ssl_certificate_key /home/ubuntu/project.key;
        error_page 497 301 =307 https://mydomain.com:5001$request_uri;

        location / {
                proxy_pass https://mydomain:5000;
                proxy_redirect off;
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Ssl on;
        }

}

ベストアンサー1

Apacheをリバースプロキシとして使用するには、サーバーに対応するモジュールがmod_proxyあり、有効になっていることを確認してください(例:)。 VirtualHostセクションの一番下に&を追加するだけです。 mod_proxy_httpsudo a2enmod proxy_httpProxyPassProxyPassReverse

その後、サーバーを再起動してくださいsudo service apache2 restart

<VirtualHost *:80>
    # added missing ServerName
    ServerName mydomain.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # should be disabled by default, just to make sure
    ProxyRequests Off

    ProxyPass /region  http://mydomain.com:5000/region
    ProxyPassReverse /region http://mydomain.com:5000/region
</VirtualHost>

<VirtualHost *:443>
        # moved ServerName to the top
        ServerName mydomain.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

        # consider using separate log files for SSL
        #ErrorLog ${APACHE_LOG_DIR}/ssl-error.log
        #CustomLog ${APACHE_LOG_DIR}/ssl-access.log combined

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        SSLEngine on
        SSLCertificateFile /home/ubuntu/project.crt
        SSLCertificateKeyFile /home/ubuntu/project.key

        # should be disabled by default, just to make sure
        ProxyRequests Off

        ProxyPass /region  http://mydomain.com:5000/region
        ProxyPassReverse /region http://mydomain.com:5000/region
</VirtualHost>

おすすめ記事