【问题标题】:Javascript in script tag doesn't execute behind nginx proxy pass脚本标签中的 Javascript 不会在 nginx 代理传递之后执行
【发布时间】:2019-06-24 10:57:36
【问题描述】:

我有一个由 Nuxtjs 构建的站点,在 nginx proxy_pass 后面运行。

假设 Nuxtjs 站点位于 http://10.001.002.003/,主站点是 http://example.com。这是 example.com 的 nginx 配置

        location /main-page/ {
            proxy_pass http://10.001.002.003/; #this is the Nuxt site
        } 

         location /api/ {
            rewrite /api/(.*) /$1  break;
            proxy_pass https://api.example.com/;
        }

        location / {
            root /home/www/html/example/dist;
            try_files $uri $uri/ /index.html;
            #index index.html;
        }

我在http://10.001.002.003/_nuxt/script1.js 有一个脚本,而该脚本又可以从http://example.com/main-page/_nuxt/script1.js 访问

问题来了,如果我浏览到http://example.com/main-page,脚本不会执行。但是,如果我浏览到http://10.001.002.003,它就可以工作。

这是html

<head>
<link rel="preload" href="http://example.com/main-page/_nuxt/script1.js" as="script">
</head>

<body>
<script src="http://example.com/main-page/_nuxt/script1.js" defer></script>
</body>

【问题讨论】:

  • 控制台有错误吗?您可以验证在网络选项卡中下载的脚本吗?
  • 控制台没有错误。是的,脚本已下载。
  • 只有其他想法... mime 类型怎么样?我认为如果控制台出错,那么控制台中会出现错误。
  • 同样的问题!您是否在 example.com 上使用 Cloudflare?

标签: javascript html nginx nuxt.js


【解决方案1】:

(1):传递请求标头:

location /main-page/ {
    proxy_pass http://10.001.002.003/; #this is the Nuxt site
    proxy_pass_request_headers on;
}

(2): 在nuxt.config.js中添加base到路由器:https://nuxtjs.org/api/configuration-router#base

router: {
    base: '/main-page/',
},

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 2016-09-22
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    相关资源
    最近更新 更多