通过nginx无法为我的goapp提供静态文件服务的问题

huangapple go评论166阅读模式
英文:

Trouble serving static files with my goapp through nginx

问题

我正在Ubuntu服务器上开发我的第一个Go应用程序。当我使用可执行文件或者简单地运行go run main.go来运行我的服务器时,我可以看到初始的HTML页面,但是没有CSS、图片或者JS。路由也会导向404页面。唯一看起来正常的是index.html(作为Go的模板命名为index.gohtml)。

当我在本地主机和服务器上使用localhost和ip:port配置运行时,所有的资源都可以加载。然而,当我使用nginx时,资源根本无法加载。我猜测这是因为nginx是我遇到问题的地方。

以下是我目前的配置。这是我第一次使用nginx,所以我不知道如何正确配置它。

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;
    }
}
英文:

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

得分: 2

你可能想将try_filesproxy_pass分别放在不同的位置块中:

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;
}

如果静态文件不存在,请求将被转发到运行在8001端口上的服务。

详细信息请参考这个文档

英文:

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.

huangapple
  • 本文由 发表于 2016年12月25日 15:49:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/41319517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定