variables - Nginx reverse proxy didnt load site correctly
问题描述
I have an nginx
configuration which listens to any subdomain *.mydomain.com
,
and I want to use subdomain as variable, to proxy request to other site.
Here is my nginx configuration
server {
listen 80;
server_name "~^(?<subdomain>.*).mydomain.com";
location / {
resolver 1.1.1.1 1.0.0.1 ipv6=off;
proxy_pass http://hosting.mydomain.com/$subdomain/;
proxy_redirect off;
access_log /var/log/nginx/proxy.log;
}
}
As I request the site directly and it loads perfectly
Site placed on AWS S3, and bucket static website address cnamed to mydomain
However, when I try to access via user1.mydomain.com, the page didn't load images, and css
And in browser network panel shows
Difference between direct and proxy access
This issue is made, because I have many sites stored in S3 bucket and located in different folders (the folder name is used as subdomain). And I want to use a single domain to access all of them via subdomains.
Thanks in advance
解决方案
您忘记代理传递 URI,您user1/index.html
为每个请求提供服务,包括 JS 和 CSS 请求,这就是为什么所有响应的大小都相同(2kb,大小user1/index.html
),这也是您进入Uncaught SyntaxError: Unexpected token <
的原因第一行,Enterprise_skeleton.bundle.js
因为它返回一个 HTML 文档,<!doctype html>
而不是实际的 JS 包。
改变
location / {
proxy_pass http://hosting.mydomain.com/$subdomain/;
}
至
location / {
proxy_pass http://hosting.mydomain.com/$subdomain$uri;
}
推荐阅读
- java - 与 JPA 保持多个 OneToMany 关系
- tfs - 在 2015 Xaml 构建中引用 BuildNumber
- android - Admob 插页式广告仅在应用中显示一次
- javascript - 在nodeJS中解析从客户端到服务器的值格式
- single-page-application - Preact 和 Parcel - 捆绑后的路由问题
- graphics - 使用组合图时控制图大小
- html - 使用引导程序禁用 div(带背景图像)
- asp.net-mvc - 使用 Html.EditorFor() 编辑/创建 Razor 页面
- java - MethodValidationInterceptor 和 @Validated @ModelAttribute
- css - 背景大小对背景颜色没有任何影响