Editing nginx site.conf file

I’m attempting to load images from production by editing the local site.conf file in site/conf/nginx.

This is what I’m using:

location ~ ^/wp-content/uploads/(.*) {
  rewrite ^/wp-content/uploads/(.*)$ http://livewebsite.com/wp-content/uploads/$1 redirect;
}

I believe I’m adding this to the correct .conf file.

When logging into the box, I can see the changes in the .conf file.

How do I provision the site to read this config? I’ve tried restarting the site and shutting down the vm, but still no luck.

1 Like

I figured it out! Here’s what I used in conf/nginx/site.conf

server {
    server_name  _;
    return 302 $scheme://local.dev$request_uri;
}

server {
    server_name ~^(.*)\.local\.dev$ local.dev;
    root /app/public/;

    index index.php;

    include do-not-modify/*.conf;

    location ~ ^/wp-content/uploads/(.*) {
            rewrite ^/wp-content/uploads/(.*)$ http://example.com/wp-content/uploads/$1 permanent;
    }

}

You’ll need to stop and start the site for the changes to take effect. Hope it helps :slight_smile:

2 Likes

This isn’t working for me, am I missing something?

server {
    server_name  _;
    return 302 $scheme://local.dev$request_uri;
}

server {
    server_name ~^(.*)\.local\.dev$ local.dev;
    root /app/public/;

    index index.php index.html index.htm;

    include do-not-modify/*.conf;

    location ~ ^/wp-content/uploads/(.*) {
        rewrite ^/wp-content/uploads/(.*)$ http://www.example.com/wp-content/uploads/$1 permanent;
    }
}
1 Like

Hi Mario,

What’s happening? Did you update the local.dev and example.com accordingly?

Yes I did, and I also restarted the site, still getting 404, I also confirmed the prod equivalent of the files exists.

Can you provide the whole site.conf file?

Also, are there any errors in the logs/nginx folder for that site?

I also ran into a similar issue and the problem was that the location needs to be put before the additional config files are loaded. Here’s what I ended up with:

server {
	server_name  _;
	return 302 $scheme://local.dev$request_uri;
}

server {
	server_name ~^(.*)\.local\.dev$ local.dev;
	root /app/public/;

	index index.php index.html index.htm;

	# Directives to send expires headers and turn off 404 error logging.
	location ~* \.(js|css|png|svg|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://example.com/$uri;
	}

	include do-not-modify/*.conf;
}
2 Likes

Another working example. This one rewrites all requests for media in the /uploads/ directory so that they reference the remote site instead.

Edit conf/nginx/site.conf to include match this, swapping out local.dev for your local URL and https://example.com for the URL of the site you want to pull images and other media from:

server {
    server_name  _;
    return 302 $scheme://local.dev$request_uri;
}

server {
    server_name ~^(.*)\.local\.dev$ campbells.dev;
    root /app/public/;

    index index.php index.html index.htm;

    # Load media in /uploads/ from remote site.
    location ~ ^/wp-content/uploads/(.*) {
        rewrite ^/wp-content/uploads/(.*)$ https://example.com/wp-content/uploads/$1 permanent;
    }

    include do-not-modify/*.conf;
}
3 Likes

This tweak really deserves an Add-on. Maybe with a switch button to quickly :rocket: enable/disable the extra location block…

3 Likes

Hi, I am new to flywheel. I am moving from vvv (vagrant) since we got wp-frontenders that are struggling with vagrant. So far so good but there is this one thing I cannot figure out: proxy_pass

I can’t seem to find the file site.conf
Anyone can help?

Hey @mvallve

The site config is managed by Local if you are using the “Preferred” Local environment. If you switch to a custom environment, you can edit the config.
54%20PM

From there if you right-click on the site name you can choose “Reveal in Finder” and the site config will be located at conf/nginx/site.conf.

Hope that helps!

I’ve taken this idea and reworked it a little for those that want to load image from production via their local site.