Ad

Deploy Gatsby Js Static Site On Nginx VPS

Basically i built my gatsby site with gatsby build command and i want to serve the public directory with all the static files with Nginx.

I have my site in /var/www/mywebsite and also some test html basic page in /var/www/test. I can serve that test page and it works fine but when i change the directory to that gatsby site it doesn't work and i get Forbiden 403 error. I assume it might be my configuration but again, do i have to do something special with gatsby to serve all those static files properly?

nginx.conf

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    index               index.html index.htm;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

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

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {
        server_name mywebsite.com www.mywebsite.com; # managed by Certbot
        # root /var/www/test;   # <-- This gets served
        root /var/www/mywebsite/public;  # <-- This doesn't
        index index.html index.html;

        location / {
            try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        # some ssl certificates...
    }

    server {
        if ($host = www.mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
        if ($host = mywebsite.com) {
            return 301 https://$host$request_uri;
        } # managed by Certbot

        listen       80;
        listen       [::]:80;
        server_name mywebsite.com www.mywebsite.com;
        return 404; # managed by Certbot
    }
}

I read that i don't need sites-available and sites-enabled directories and that it can be configured here in the main nginx.conf file or included from that conf.d directory in the nginx.conf itself.

Ad

Answer

Forbidden error could be related to a permission problem.
You can check the permission by running ls -al in /var/www.

Run sudo chown www-data -R /var/www and see if the problem still occurs. If this doesn't fix the problem run:

sudo tail -f /var/log/nginx/error.log

And check the logs to get an idea what's the problem. or Inlcude the logs in your question.

Ad
source: stackoverflow.com
Ad