1

ORIGINAL

I had angular (version: 7) site which was deployed on S3 bucket, but for SEO purposes, recently I integrated Angular Universal.

Now I moved site/frontend from S3 bucket to EC2 instance. I am trying to deploy my angular universal application on Nginx (version: nginx/1.14.1) on my EC2 instance (CentOS).

The flow goes something like domain-name(example.com/www.example.com) is registered on godaddy which points out to Cloud Front which sends/forward my request to EC2 instance (Nginx server).

Issue

Whenever I try to hit the urls https://example.com or https://www.example.com, they got redirected to EC2 instance address like https://ec2-xx-xxx-xxx-xx.region-x.compute.amazonaws.com/ .

Someone figured out that it is due to server configuration, as stated here

Be sure, when you connect through CloudFront, that the server doesn't redirect you back to the EC2 hostname or IP (the address bar in the browser will change, if it does, and you'll want to fix your web server's config if that happens).

So it seems like I have nginx configuration issue. Following are the confurations I used.

EC2 Security Group Settings

EC2 Security Group

nginx.conf

upstream nrc_frontend_nodejs {
    server 127.0.0.1:4000;
}

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}

server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;
    server_name  example.com www.example.com;

    ssl_certificate ssl/example.chained.crt;
    ssl_certificate_key ssl/example.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'CEMDH+ATSGCM:EDH+AESGCG:AES256+EECDH:AES256+EDH';

    root /home/ec2-user/nginx/dist; # <-- Static Resources

   # Load configuration files for the default server block.
   include /etc/nginx/default.d/*.conf;

   location / {
        try_files $uri $uri @backend; # <--- This looks for requests (statics i.e js/css/fonts)
                                      # in /ssr/dist folder. If nothing found, calls @backend (running at 127.0.0.1:4000)
    }

    location @backend {
        proxy_pass http://nrc_frontend_nodejs;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_http_version 1.1;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass_request_headers on;
    }
}

Its been couple of days, I have tried many different ways to configure the nginx, but no luck so far. Can anyone help ?

EDIT

I have simplified the nginx.conf file further more. Just pointing to the default nginx static html page (no reverse proxy, no fancy configurations)

nginx.conf

server {
        listen 80;
        listen [::]:80;
        server_name _;
        return 301 https://$host$request_uri;
}


server {
    listen       443 ssl http2 default_server;
    listen       [::]:443 ssl http2 default_server;
    server_name  example.com www.example.com;

    ssl_certificate ssl/example.chained.crt;
    ssl_certificate_key ssl/example.key;

    root /usr/share/nginx/html;
   # Load configuration files for the default server block.
   include /etc/nginx/default.d/*.conf;

   location / {
   }

}

But still whenever I hit https://www.example.com it redirects to EC2 instance, then show the nginx's default index.html file.

I am not sure if its Nginx issue or AWS (CloudFront or EC2) issue. Can anyone let me know the solution?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.