Trouble serving static files with my goapp through nginx

847 views Asked by At

I am making my first go app on an Ubuntu Server. When I run my server using either an executable or simply go run main.go, I get the initial html page to render, but none of the css, images, or js. The routes also take me to a 404 page. The only thing that seems to pass is the index.html(which is named index.gohtml as a template for go)

All my assets are loaded when I run it on localhost and ip:port configuration on the server, however when I use nginx the assets are not loading in at all. I am assuming because of these factors that nginx is where I am coming across my issue.

Below is what I have so far. This is my first time using nginx so I am unaware of what is necessary to configure it properly.

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    root /var/www/html;

    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {

            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:8001;

            try_files $uri $uri/ =404;

    }

}

1

There are 1 answers

1
Richard Smith On

You might want to separate the try_files and proxy_pass into separate locations:

location / {
    try_files $uri $uri/ @proxy;
}
location @proxy {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8001;
}

If the static file does not exist, the request will be forwarded to the service running on port 8001.

See this document for details.