首页 > 解决方案 > 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

This is the same site

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

标签: variablesnginxstaticproxypassserver-name

解决方案


您忘记代理传递 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;
}

推荐阅读