2

Say I'm accessing www.mywebsite.com.

This website fetches the following asset:

http://www.mywebsite.com/styles/app.css

I want to access the website exactly as I normally would, with one exception:

Whenever my browser makes a request to /styles/app.css, instead of fetching it from http://www.mywebsite.com, I want to fetch it from http://localhost:3000/mywebsite/.

So instead it should be fetching:

http://localhost:3000/mywebsite/styles/app.css

Is this possible with nginx?

I tried to do it using the following server config:

{
    ...
    server {
        listen       80;
        server_name  mywebsite.com;

        location /styles/ {
            proxy_pass http://localhost:3000/mywebsite/styles/;
        }
}

But even after restarting nginx (sudo nginx -s quit, sudo nginx), nothing seems to have changed.

When I browse to www.mywebsite.com/styles/app.css, I still get the same old app.css being retrieved from the server, rather than my local one.

Jonathan
  • 32,202
  • 38
  • 137
  • 208
  • I found a similar question [here](http://stackoverflow.com/questions/12806893/use-nginx-to-serve-static-files-from-subdirectories-of-a-given-directory). But the key difference is that I'm trying to redirect traffic to a local HTTP server, whereas the other questioner is trying to serve from the file-system. So I don't believe this qualifies as a duplicate question. – Jonathan May 04 '15 at 02:26
  • Did you change your DNS record to point to Localhost? If you haven't, going to my website.com will always go to the remote server – Kyle May 04 '15 at 06:53
  • Thanks @kyl191. I assume this involves editing my hosts file? Didn't realise that nginx doesn't have this capability out of the box. My only concern then is whether nginx can access the remote site either, since the hosts file might also apply to nginx. I'll experiment and see what happens. – Jonathan May 04 '15 at 07:18
  • 1
    Not necessarily. You could add an extra a record to your DNS server (something like local.mywebsite.com) with the address 127.0.0.1, and have nginx listen for that domain. It would also get around nginx not being able to connect to the remote site. In all honesty though, you're probably better off with squid, which is designed to be a proxy. – Kyle May 05 '15 at 04:16
  • Ah I thought you were referring to some local setting. Unfortunately I don't have any control over the actual DNS itself. It's controlled by a separate company. I did manage to get the effect I wanted using [Charles Proxy](http://www.charlesproxy.com/). But it would've been nicer to use something FOSS, such as nginx. I gave [mitmproxy](http://mitmproxy.org) a go, but was intimidated by the amount of deep networking config that it would require. I'll check out squid, maybe that's a better tool. – Jonathan May 05 '15 at 05:00

0 Answers0