Valet: Proxy missing media from production in nginx configuration

Created on 10 Dec 2016  路  4Comments  路  Source: laravel/valet

With the switch to nginx this became more possible and I would like to integrate this for WordPress uploads and perhaps other drivers. It may be best as a plugin but do you think this could serve others well if added to the core commands like secure.

When a file isn't found locally it tries the production location for the uri.

The command setting this up would be something like valet proxy [productiondomain.com] if the domain is omitted it would assume folder pathname with .com

and the config would be similar to this from the VVV site wizard

# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpe?g|gif|ico)$ {
    expires 24h;
    log_not_found off;
    try_files $uri $uri/ @production;
}

location @production {
    resolver 8.8.8.8;
    proxy_pass http://productiondomain.com/$uri;
}

Most helpful comment

@hybridvision

Just in case you still need it, here is a working version:

Be sure to:

  1. replace your-username-here with the username in your local env
  2. replace site-name with the site name
  3. replace example.com with the remote URL
location ~* .(png|jpe?g|gif|ico|svg)$ {
        expires 24h;
        log_not_found off;
        root '/Users/your-username-here/.valet/Sites/site-name/';
        if (-f $request_filename) {
            break;
        }
        try_files $uri $uri/ @production;
    }
    location @production {
        resolver 8.8.8.8;
        proxy_pass https://example.com/$uri;
    }

All 4 comments

I have been trying to get this to work and if I add the above code to the nginx configuration for one of my sites manually and then restart Valet, I get a 502 Bad Gateway response. I'm using valet 2.0.3, freshly updated today.

I tried some other configurations like replacing proxy_pass with a redirection by doing:

location @production {
    return 302 https://www.example.com$uri;
}

This works but it redirects _everything_, not just missing files. It's almost as if the try_files line is not recognising that any of the files exist locally, although they clearly do. I've spent far too long on this and it's a mystery... I guess it must be related to some other part of the configuration for nginx in Valet but I'm still a beginner with nginx.

Has anyone managed to make this work properly?

@hybridvision

Just in case you still need it, here is a working version:

Be sure to:

  1. replace your-username-here with the username in your local env
  2. replace site-name with the site name
  3. replace example.com with the remote URL
location ~* .(png|jpe?g|gif|ico|svg)$ {
        expires 24h;
        log_not_found off;
        root '/Users/your-username-here/.valet/Sites/site-name/';
        if (-f $request_filename) {
            break;
        }
        try_files $uri $uri/ @production;
    }
    location @production {
        resolver 8.8.8.8;
        proxy_pass https://example.com/$uri;
    }

Hi @chood531, after a long gap, I needed this again today so I came searching and I found your reply that I'd missed earlier (thanks!).

In the end, I solved it with a custom driver that I found here: https://github.com/Briteweb/wordpress-proxy-valet-driver - I found this was cleaner and easier than having to edit nginx configs.

This can be closed.

Working solutions are posted above, and another alternate is mentioned in https://github.com/laravel/valet/issues/729

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hsleewis picture hsleewis  路  5Comments

sadhakbj picture sadhakbj  路  3Comments

webartistse picture webartistse  路  4Comments

eberkund picture eberkund  路  4Comments

LucidNinja picture LucidNinja  路  4Comments