Home  >  Q&A  >  body text

golang - nginx as reverse proxy go as server cannot read static files

There is the following code in the website html:

<link rel="stylesheet" href="/static/css/bootstrap.min.css" />
<link rel="stylesheet" href="/static/css/bootstrap-theme.min.css" />
<link rel="stylesheet" href="/static/css/jquery.treegrid.css" />
<link rel="stylesheet" href="/static/css/browseraudit.css" />

The problem now is that the browser cannot read these files
Use go as the server and nginx as the reverse proxy
The html files are placed in the /home/user/project/ directory
static and other folders Also placed in this directory
/home/user/project/ is the project root directory

nginx has made the following configuration

location /static/ {
    alias /home/user/project/static/;
}

But it doesn’t feel like it’s working

How can I make the browser correctly find the specified file without modifying the original code of the web page?

大家讲道理大家讲道理2597 days ago1010

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-05-16 17:18:46

    拿一个beego的nginx配置文件给你参考,大概原理是除了指明URL里带有的css,js,fonts,img字符的nginx接管,如果文件不存在都转发到后端请求,相当于GO开启的web服务器

    server {
        listen       80;
        server_name  .a.com;
    
        charset utf-8;
        access_log  /home/a.com.access.log;
    
        location /(css|js|fonts|img)/ {
            access_log off;
            expires 1d;
    
            root "/path/to/app_a/static";
            try_files $uri @backend;
        }
    
        location / {
            try_files /_not_exists_ @backend;
        }
    
        location @backend {
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host            $http_host;
    
            proxy_pass http://127.0.0.1:8080;
        }
    }

    reply
    0
  • Cancelreply