2017-09-28 2 views
0

J'ai une application Spring Boot fonctionnant sur Beanstalk et je voulais récemment sécuriser tout mon site via HTTPS, donc je voudrais rediriger tout le trafic HTTP vers HTTPS par défaut.Configurer la redirection HTTP vers HTTPS pour nginx d'AWS Beanstalk dans l'application Spring Boot

J'ai déjà installé mon certificat SSL avec Amazon Certificate Manager et il est utilisé par mon équilibreur de charge Amazon ELB, de sorte que le HTTPS se termine là.

Actuellement, l'équilibrage de charge est configuré avec les ports mapping comme celui-ci: enter image description here

J'ai aussi remarqué que, par défaut il y a aussi un nginx sur l'équilibreur de charge qui écoute sur le port 80 (par exemple le port), puis vers l'avant à mon application Spring Boot enfin.

J'ai donc essayé de faire la redirection en mettant ce fichier conf à .ebextensions/nginx/conf.d/elasticbeanstalk/00_nginx_https_rw.conf et le dossier .ebextensions se trouve localement sous src/main/resources dans mon repo Spring Boot:

files: 
    "/tmp/45_nginx_https_rw.sh": 
    owner: root 
    group: root 
    mode: "000644" 
    content: | 
    #! /bin/bash 

    CONFIGURED=`grep -c "return 301 https" /opt/elasticbeanstalk/support/conf/webapp_healthd.conf` 

    if [ $CONFIGURED = 0 ] 
    then 
     sed -i '/listen 80;/a \ if ($http_x_forwarded_proto = "http") { return 301 https://$host$request_uri; }\n' /opt/elasticbeanstalk/support/conf/webapp_healthd.conf 
     logger -t nginx_rw "https rewrite rules added" 
     exit 0 
    else 
     logger -t nginx_rw "https rewrite rules already set" 
     exit 0 
    fi 

container_commands: 
    00_appdeploy_rewrite_hook: 
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/appdeploy/enact 
    01_configdeploy_rewrite_hook: 
    command: cp -v /tmp/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact 
    02_rewrite_hook_perms: 
    command: chmod 755 /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh 
    03_rewrite_hook_ownership: 
    command: chown root:users /opt/elasticbeanstalk/hooks/appdeploy/enact/45_nginx_https_rw.sh /opt/elasticbeanstalk/hooks/configdeploy/enact/45_nginx_https_rw.sh 
    04_reload_nginx: 
    command: "sudo service nginx reload" 

Je déployé mon application Boot Spring avec ce fichier de configuration et également fait « Redémarrer App Server (s) » dans Beanstalk, mais il ne parvient toujours pas rediriger de HTTP vers HTTPS

J'ai aussi essayé cela pour mon fichier conf aussi bien et cela ne fonctionne pas:

listen 80; 

# ELB stores the protocol used between the client 
# and the load balancer in the X-Forwarded-Proto request header. 
# Check for 'https' and redirect if not 
if ($http_x_forwarded_proto != 'https') { 
    rewrite^https://$host$request_uri? permanent; 
} 


server_name mothersquad.com www.mothersquad.com 

C'est là que je mets mon fichier de configuration: enter image description here

C'est mon Nginx access.log lorsque je tente d'aller à la version HTTP de mon site:

172.31.42.155 - - [28/Sep/2017:07:38:53 +0000] "GET /health_check HTTP/1.1" 200 2 "-" "ELB-HealthChecker/1.0" "-" 
172.31.42.155 - - [28/Sep/2017:07:39:03 +0000] "GET /health_check HTTP/1.1" 200 2 "-" "ELB-HealthChecker/1.0" "-" 
172.31.42.155 - - [28/Sep/2017:07:39:13 +0000] "GET /health_check HTTP/1.1" 200 2 "-" "ELB-HealthChecker/1.0" "-" 
172.31.42.155 - - [28/Sep/2017:07:39:23 +0000] "GET /health_check HTTP/1.1" 200 2 "-" "ELB-HealthChecker/1.0" "-" 
172.31.42.155 - - [28/Sep/2017:07:39:23 +0000] "GET/HTTP/1.1" 200 93279 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:23 +0000] "GET /css/landing.css HTTP/1.1" 200 13132 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:23 +0000] "GET /css/landing_bootstrap.css HTTP/1.1" 200 134640 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:23 +0000] "GET /js/landing.js HTTP/1.1" 200 5627 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/logo.png HTTP/1.1" 200 6830 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/tracery.png HTTP/1.1" 200 23045 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-4.jpg HTTP/1.1" 200 17441 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-1.jpg HTTP/1.1" 200 24258 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-2.jpg HTTP/1.1" 200 20504 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-5.jpg HTTP/1.1" 200 18711 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-3.jpg HTTP/1.1" 200 20686 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/virtualgroup.jpg HTTP/1.1" 200 46406 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-6.jpg HTTP/1.1" 200 21364 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/squad-photo-7.jpg HTTP/1.1" 200 18472 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/tracery-red.png HTTP/1.1" 200 2500 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/bg1.jpg HTTP/1.1" 200 48181 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 
172.31.42.155 - - [28/Sep/2017:07:39:24 +0000] "GET /images/landing/bg2.jpg HTTP/1.1" 200 116554 "http://www.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "172.113.254.101" 

Je n'ai pas vu des erreurs dans les erreurs.log

Quoi d'autre ai-je manqué? Merci

Répondre

0

Si vous avez installé votre certificat sur l'équilibreur de charge, je recommande de terminer SSL sur le LB (en supposant que votre VPC est sécurisé). Essayez de définir le paramètre server.use-forward-headers dans les propriétés de votre application sur true. Cela entraînera votre application de démarrage à respecter les en-têtes X-Forwarded-Proto et X-Forward-For.

Une fois cette opération terminée, retirez le port 80 de votre écoute LB afin que votre application ne peut être consulté sur 443.

+0

Salut Lane, merci, donc si je fais ce changement server.use-forward-têtes, que signifie que je devrais revenir toutes mes extensions .nextensions nginx? Juste pour m'assurer que je comprends bien, au moment où je garde seulement le port 443 et pas 80, tous les appels http redirigera automatiquement vers https? – user1805458

+0

Conservez les extensions et le port si vous souhaitez la redirection. Notez également que vous devrez définir cette propriété dans votre fichier de propriétés ou dans toute autre source de propriété externe. Vous ne pouvez pas le définir dans les propriétés EBS en raison des tirets. –

+0

Le problème est que ma config nginx ne semble pas encore faire la redirection http, et je voudrais d'abord résoudre ce problème. Y a-t-il quelque chose d'incorrect dans mon fichier conf? – user1805458