Nginx unable to render static files from Mojolicous

55 Views Asked by At

Using morbo, the static files are rendered for all pages. However with the hypnotoad server using Nginx as a proxy, a page is loaded but static files within are not rendered – not even the favicon, but calling a static html page within the same app, both images and favicon appear. What maybe missing?

   upstream backendurl {
    server 127.0.0.1:8080  fail_timeout=0;
}

server {
  listen  80;
  listen [::]:80;
  server_name example.com www.example.com ;

  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log info;
  root /var/www/example.com/public;
 
  location / {
    try_files $uri @proxy;
    access_log off;
    expires max;
    add_header 'Access-Control-Allow-Origin' 'http://example.com';
  }
  

  location @proxy {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass       http://backendurl;
  }
}
1

There are 1 best solutions below

0
On

The Apache2 solution gave some hints: https://github.com/mojolicious/mojo/wiki/Apache-deployment
A successful Nginx solution was found as follows:

upstream backendurl {
    server 127.0.0.1:8080  fail_timeout=0;
}
 
server {
  listen  443 ssl;
  listen [::]:443 ssl;
  server_name myexample.com www.myexample.com;
 
  access_log /var/log/nginx/access.log;
  error_log  /var/log/nginx/error.log info;
  root /home/sammy/simple_image/public;
  
  location / {
    try_files $uri @proxy;
    access_log off;
    expires max;
    add_header 'Access-Control-Allow-Origin' 'https://myexample.com';
  }
  
    location /static {
    try_files $uri @proxy;
    access_log off;
    expires max;
    add_header 'Access-Control-Allow-Origin' 'https://myexample.com';
  }
 
  location @proxy {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass       http://backendurl;
  }
  
    ssl_prefer_server_ciphers on;
      #add all the necessary ssl files, (eg .crt and key files) and links here
}