title: Deploy Gatsby js static site on Nginx VPS
body: <p>Basically i built my gatsby site with <code>gatsby build</code> command and i want to serve the <code>public</code> directory with all the static files with <code>Nginx</code>.</p>
  
  <p>I have my site in <code>/var/www/mywebsite</code> and also some test html basic page in <code>/var/www/test</code>.
  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 <code>Forbiden 403 error</code>.
  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?</p>
  
  <p><strong>nginx.conf</strong></p>
  
  <pre class="lang-sh prettyprint-override"><code>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;   # &lt;-- This gets served
          root /var/www/mywebsite/public;  # &lt;-- 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
      }
  }
  </code></pre>
  
  <p>I read that i don't need <code>sites-available</code> and <code>sites-enabled</code> 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.</p>