golang - nginx作为反向代理go作为服务器无法读取静态文件

【字号: 日期:2022-07-18浏览:30作者:雯心

问题描述

网站html中有如下代码:

<link rel='stylesheet' href='https://www.6hehe.com/static/css/bootstrap.min.css' /><link rel='stylesheet' href='https://www.6hehe.com/static/css/bootstrap-theme.min.css' /><link rel='stylesheet' href='https://www.6hehe.com/static/css/jquery.treegrid.css' /><link rel='stylesheet' href='https://www.6hehe.com/static/css/browseraudit.css' />

现在问题是浏览器无法读取到这些文件 使用go作为服务器,nginx作为反向代理html文件放在 /home/user/project/目录下static等文件夹也放在该目录下/home/user/project/是项目根目录

nginx做了如下配置

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

但是感觉没有起到作用

请问如何在不修改网页原本代码的前提下使浏览器正确找到指定的文件?

问题解答

回答1:

拿一个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; }}

相关文章: