简体   繁体   中英

Issue rewriting HTTP url to HTTPS on Amazon EC2

I am trying to make my site accept secured HTTPS urls only.

I have a node web application currently deployed to Elastic Beanstalk (Health - OK). The website works fine at this point.

I added a load balancer in front my EC2 instance(ssl cert added) The listener is configured as follows:

Load Balancer Protocol: HTTPS Port: 443 - Instance Protocol HTTP: Instance Port: 80
Load Balancer Protocol: HTTP Port: 80 - Instance Protocol HTTP: Instance Port: 80

Apparently EC2 load balancers do not support HTTP redirects to HTTPS. So I decided to follow these instructions .

I updated my nginx.conf and added the following in the HTTP section:

server {
    listen         80;
    server_name    mysite.com;
    if ($http_x_forwarded_proto = 'http') {            
        return 301 https://$server_name$request_uri
    }
}

I rebooted my instance and it's currently in service. When I try to access a url path the redirect works however all pages are giving a 404 error.

I checked the logs on my instance and found the following:

2018/03/09 00:02:27 [error] 3533#0: *57 open() "/usr/share/nginx/html/channel/create" failed (2: No such file or directory), client: 172.31.57.30, server: mysite.com, request: "GET /channel/create HTTP/1.1", host: "mysite.com"

2018/03/09 00:02:28 [error] 3533#0: *57 open() "/usr/share/nginx/html/channel/create" failed (2: No such file or directory), client: 172.31.57.30, server: mysite.com, request: "GET /channel/create HTTP/1.1", host: "mysite.com"

When I log in to the instance I can see my deployment files in var/app/current with my node.js index file

The url path in my browser is definitely correct. Can anyone tell me why the instance is looking for files in /usr/share/nginx/html/?

/usr/share/nginx/html/ currently only has the default Nginx files.

# Elastic Beanstalk Managed

# Elastic Beanstalk managed configuration file
# Some configuration of nginx can be by placing files in /etc/nginx/conf.d
# using Configuration Files.
# http://docs.amazonwebservices.com/elasticbeanstalk/latest/dg/customize-containers.html 
# 
# Modifications of nginx.conf can be performed using container_commands to modify the staged version
# located in /tmp/deployment/config/etc#nginx#nginx.conf

# Elastic_Beanstalk
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

http {

    port_in_redirect off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    server {
      listen         80;
      server_name    mysite.com;
      if ($http_x_forwarded_proto = 'http') {            
        return 301 https://$server_name$request_uri;
        }
}

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65; 
# Elastic Beanstalk Modification(EB_INCLUDE)

    log_format healthd '$msec"$uri"'
                       '$status"$request_time"$upstream_response_time"'
                       '$http_x_forwarded_for';

include /etc/nginx/conf.d/*.conf;
# End Modification
}

您必须通过此处记录的elasticbeanstalk流程编辑nginx配置,对于通过beantalk进行部署的任何编程语言都是相同的。

This worked after changing the location of my node index.js. Updated the Nginx.conf with the following location:

      location / {
      root /var/app/current;
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM