0

I've developed an Angular 2 app on my mac and have it running locally using Nginx as the websever. I'm having trouble now that I'm deploying this onto an AWS EC2 instance. It's a Linux Red Hat 7 instance.

I've installed Nginx and copied my working config from my mac, making sure to change the server_name value and root, but I either get the default nginx page or 404 errors.

I have setup my app at /var/www/dist and given chmod 755 permissions to that path and files in dist. Below is my config (note the angle brackets for server_name aren't in the actual config, I'm just hiding the instance name for this post).

Any attempt at myhost/index.html or myhost/someapppath all return 404. Using myhost/ returns the default nginx page. I've even tried checking my root by creating the html dir and putting a file there to test but even that doesn't get picked up. I've restarted nginx after each config too but still get the same.

Any ideas?

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 128;
    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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  <hiding instance for this post>.ap-southeast-2.compute.amazonaws.com;
        root /var/www/dist;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            index  index.html index.htm;
            try_files $uri $uri$args $uri/index.html /index.html =404;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
       error_page   500 502 503 504  /50x.html;
       location = /50x.html {
         root   html;
       }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
#include /etc/nginx/conf.d/*.conf;
}

UPDATE

After tinkering with the config again and then looking at the error logs I now see the following:

2017/11/16 17:12:14 [crit] 23126#23126: *2 stat() "/var/www/dist/index.html" failed (13: Permission denied)

This makes more sense. I've ran sudo chown nginx: var/ and sudo chown -R nginx: /var/www for the nginx user and restarted nginx but I still get permission denied. Am I setting the permissions correctly?

rossco
  • 523
  • 4
  • 20
  • I'd love to help! Did you place your index into /dist? Also server name should be the URL that a user will request – Colin Nov 16 '17 at 13:17
  • @Colton index.html is in the /dist folder and the server_name is the public DNS name that I use to access the site. Weird, it all works locally. – rossco Nov 16 '17 at 21:41

1 Answers1

0

Fixed it, very tricky issue. Solved using comment from @jsina in this post: https://stackoverflow.com/a/36389001/2313746.

Effectively, if you mv a directory from /home it doesn't update the context for permissions.

rossco
  • 523
  • 4
  • 20