nginx 역방향 프록시를 사용하여 동일한 포트에서 http에서 https로 리디렉션

nginx 역방향 프록시를 사용하여 동일한 포트에서 http에서 https로 리디렉션

http://mydomain.com/myroject우분투 14.04(아파치 웹서버)에 웹사이트를 구축했습니다.

동일한 호스트의 포트 5000에서 실행되는 htsql 서비스를 설정했습니다.

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

그런 다음 이 서버에 SSL 인증서를 설치하여 웹 사이트를 실행했습니다.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>

관련 정보