Advertisement:

Author Topic: Proxy Images without Proxy.php (Nginx Users)  (Read 962 times)

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 153
Proxy Images without Proxy.php (Nginx Users)
« on: February 18, 2018, 04:30:46 PM »
If you're using Nginx, here is how to proxy images without calling the Proxy.php file.

Using this method will prevent expensive calls to PHP on Apache, PHP-FPM, etc to show the image. Nginx can handle this using a lot less ram and resources.

To make this work, you will need to modify your nginx website vhost file + modify 3 forum files.

In your Nginx website's vhost file, add this at the bottom :

Code: [Select]
location ~* /proxy/ {

# prevent search engines from caching your proxy url's
add_header X-Robots-Tag "noindex, nofollow";

# return a 404 error if the image type is not allowed
        # adjust the allowed image types below
if ($args !~ "^(?:http:\/\/.+\.(?:png|jpe?g|gif|bmp|ico|svg).*)") { return 404; }

# use Google DNS servers to resolve domain names + cache results for 5 minutes
resolver 8.8.8.8 8.8.4.4 valid=300s;

# allow several redirects using the error_page directive
recursive_error_pages on;

# if the image no longer exists, redirects or throws an error - handle them
proxy_intercept_errors  on;
error_page      301 302 307 = @proxy_redirects;
error_page      404 = @proxy_not_found;

        # everything looks good - let's grab the image
proxy_pass      $args;
}

location @proxy_redirects {

set $image_loc   $upstream_http_location;

add_header X-Robots-Tag "noindex, nofollow";

resolver 8.8.8.8 8.8.4.4 valid=300s;

proxy_intercept_errors  on;

        # the image redirected to a new URL - let's grab the new URL and process it again
error_page      301 302 307 = /proxy/?$image_loc;

        # the image does not exist - throw a 404 error
error_page      404 = @proxy_not_found;

        # let's grab the image
proxy_pass      $image_loc;
}

location @proxy_not_found {
return 404;
}

Restart Nginx and let's test to make sure what we have now is working. Modify example.com to your own domain name.

https://example.com/proxy/?http://png.icons8.com/metro/1600/reddit.png

If you see the reddit logo, it's working! If not, confirm you have the code in the correct vhost file and that the Nginx server was restarted.

Now let's modify the forum files. Change all instances of example.com to your domain name!

Sources/Load.php :

Find:
Code: [Select]
$user_settings['avatar'] = strtr($boardurl, array('http://' => 'https://')) . '/proxy.php?request=' . urlencode($user_settings['avatar']) . '&hash=' . md5($user_settings['avatar'] . $image_proxy_secret);Replace:
Code: [Select]
$user_settings['avatar'] = 'https://example.com/proxy/?' . $user_settings['avatar'];
Find:
Code: [Select]
$row['avatar'] = $boardurl . '/proxy.php?request=' . urlencode($row['avatar']) . '&hash=' . md5($row['avatar'] . $image_proxy_secret);Replace:
Code: [Select]
$row['avatar'] = 'https://example.com/proxy/?' . $row['avatar'];
            
Sources/Subs.php :

Find both instances of:
Code: [Select]
$data = $boardurl . '/proxy.php?request=' . urlencode($data) . '&hash=' . md5($data . $image_proxy_secret);Replace them with:
Code: [Select]
$data = 'https://example.com/proxy/?' . $data;
Find:
Code: [Select]
$context['user']['avatar']['href'] = $boardurl . '/proxy.php?request=' . urlencode($user_info['avatar']['url']) . '&hash=' . md5($user_info['avatar']['url'] . $image_proxy_secret);Replace:
Code: [Select]
$context['user']['avatar']['href'] = 'https://example.com/proxy/?' . $user_info['avatar']['url'];
               
Sources/Profile-Modify.php :

Find:
Code: [Select]
if ($image_proxy_enabled && !empty($cur_profile['avatar_original']) && $cur_profile['avatar_original'] != $cur_profile['avatar'] && stristr($cur_profile['avatar'], $boardurl . '/proxy.php'))

Replace :
Code: [Select]
if ($image_proxy_enabled && !empty($cur_profile['avatar_original']) && $cur_profile['avatar_original'] != $cur_profile['avatar'] && stristr($cur_profile['avatar'], 'https://example.com/proxy/'))


To enable the proxy, open the forum/Settings.php file  :

Find:
Code: [Select]
$image_proxy_enabled = 0;
Replace:
Code: [Select]
$image_proxy_enabled = 1;

If you have any questions or improvement suggestions, please share them below!
« Last Edit: February 21, 2018, 09:57:49 PM by MobileCS »

Offline Suki

  • Developer
  • SMF Super Hero
  • *
  • Posts: 15,223
  • Kaizoku Jotei
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #1 on: February 21, 2018, 01:11:56 PM »
Seems good, you don't really need to do the !$image_proxy_enabled change since you are inverting the effect that setting has, making it more complicated for the user than it needs to be.  Just leave the setting as it is, if the user enables it then nginx will kick in, if not, then nothing will happen.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 153
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #2 on: February 21, 2018, 04:50:02 PM »
The only issue with that is I'm doing a "find / add after" (not replace) on all the edits.

I wanted to still have proxy.php support available for the forum. That is why I am using "!$image_proxy_enabled" - so the user can $image_proxy_enabled = 0; for Nginx proxy, and $image_proxy_enabled = 1; for the forum built in proxy.

I'm not sure to make it less confusing without having to add an additional 8 edits. (set special variable in settings.php + global that variable in 7 locations).

Offline Suki

  • Developer
  • SMF Super Hero
  • *
  • Posts: 15,223
  • Kaizoku Jotei
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #3 on: February 21, 2018, 05:12:59 PM »
Well, this is a trick specifically for nginx users and those who would want to implement the trick won't have any need for the regular SMF proxy, if they still had any need for the regular proxy then they wouldn't be implementing this trick ;)

Having both options available seems redundant. It seems simpler to just replace the regular proxy with ngnix, leaving the setting logic intact.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline MobileCS

  • Jr. Member
  • **
  • Posts: 153
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #4 on: February 21, 2018, 09:50:04 PM »
Well, this is a trick specifically for nginx users and those who would want to implement the trick won't have any need for the regular SMF proxy, if they still had any need for the regular proxy then they wouldn't be implementing this trick ;)

I really can't argue that! I've updated my code to your new recommendations.  :)

Handling it your method also requires less template editing. So it's much easier and cleaner to implement.

Offline Suki

  • Developer
  • SMF Super Hero
  • *
  • Posts: 15,223
  • Kaizoku Jotei
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #5 on: February 21, 2018, 10:34:54 PM »
Cool!
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

Look at them. They're just asking for it. Maybe the human race deserves to be wiped out.

Offline albertlast

  • Development Contributor
  • Jr. Member
  • *
  • Posts: 276
Re: Proxy Images without Proxy.php (Nginx Users)
« Reply #6 on: February 25, 2018, 10:23:13 AM »
A small note,
this feature would be intraged in smf2.1 (when nothing goes wrong): https://github.com/SimpleMachines/SMF2.1/pull/4565