DEV Community

Avinash Hegde
Avinash Hegde

Posted on

Nginx: How to redirect JS/CSS HTTP links to other domain?

I have few HTTP links to the js/css files. I wanted to redirect it to other location. Example provided below:

Redirection from:

https://ex.example.com/js/file1.js

https://ex.example.com/css/file2.css

https://ex.example.com/css/folder1/file3.css

Redirection to:

https://ex2.example.com/js/file1.js

https://ex2.example.com/css/file2.css

https://ex2.example.com/css/folder1/file3.css

I have tried below settings in nginx.conf. But its not working.

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;
Enter fullscreen mode Exit fullscreen mode

}
}
Please help me with this.

Top comments (0)