Nginx:如何将JS/CSS http链接重定向到其他域?

icomxhvb  于 5个月前  发布在  Nginx
关注(0)|答案(2)|浏览(68)

我有几个HTTP链接到js/css文件.我想重定向到其他位置.下面提供的例子:
重定向自:
https://ex.example.com/js/file1.js
https://ex.example.com/css/file2.css
https://ex.example.com/css/folder1/file3.css
重定向至:
https://ex2.example.com/js/file1.js
https://ex2.example.com/css/file2.css
https://ex2.example.com/css/folder1/file3.css
我在nginx. conf中尝试了下面的设置。但它不起作用。

server {
    listen       443 ssl;
    server_name ex.example.com;

    location / {
       return 301 https://ex2.example.com;
    }
    location ~* \.(css)$ {
        root https://ex2.example.com/css;
    }
    location ~* \.(js)$ {
        root https://ex2.example.com/js;
    }
    #location /css/ {
    #   return 301 https://ex2.example.com/css;
    #}
    #location /js/ {
    #   return 301 https://ex2.example.com/js;
    #}

    ssl_certificate      /etc/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/myserver.key;
    client_max_body_size 5M;
    ssl_prefer_server_ciphers   on;

  }
}

字符串
请帮帮我

mfuanj7w

mfuanj7w1#

使用以下配置:

server {
    listen       443 ssl;
    server_name ex.example.com;

    location / {
       return 301 https://ex2.example.com/$request_uri;
    }

    ssl_certificate      /etc/ssl/ssl-bundle.crt;
    ssl_certificate_key  /etc/ssl/myserver.key;
    client_max_body_size 5M;
    ssl_prefer_server_ciphers   on;

  }
}

字符串
/$request_uri会自动获取前一个请求的URI,并将其添加到重定向URI中。你不需要匹配正则表达式和单独的重定向。

yfwxisqw

yfwxisqw2#

如下面的链接
rewriting urls for static js and css files using nginx server
如果你只想重定向js和css文件,使用这个:

rewrite /(.*)\.(css|js)(.*)$ https://ex2.example.com/$1.$2$3 last;

字符串

相关问题