Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Serving images/assets from static web server
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Serving images/assets from static web server

DrukpaDrukpa Member
edited February 2013 in Help

I have a web app that allows users to do many things, including uploading of images to galleries. The web server is Apache. Since apache consumes alot of memory/resources even when serving static content, I am thinking of serving the images and static content from a Nginx web server on another VPS. I don't want to use Nginx to process PHP and I don't want to use any third-party CDN for now.

Ex:
Webapp : www.domain.com
Static Web Server : static.domain.com

The problem is, when a user uploads an image, how do I transfer it to the Nginx server? How do you guys do it?

Comments

  • scp ?

  • you might be able to use unison, or glusterfs to sync the files from the main webserver to the static server.

    The down side of this is the files would be on both servers.

  • @Mun said: you might be able to use unison, or glusterfs to sync the files from the main webserver to the static server.

    The down side of this is the files would be on both servers.

    Will the files be synced instantly? When a user uploads an image, it should be able to show him the uploaded image served from the Nginx server.

  • rm_rm_ IPv6 Advocate, Veteran

    Same directory on the filesystem visible by both web servers (use symlinks if you must).

  • @rm_ said: Same directory on the filesystem visible by both web servers (use symlinks if you must).

    How can that be possible if they are on two different VPS.

    I believe, for two webservers to share the same filesystem, they will have to be on the same server/vps. If on a same server, then two web servers cannot listen on port 80, can they?

  • nfnnfn Veteran

    Why don't you place nginx in front (proxy) of apache?

  • arieonlinearieonline Member
    edited February 2013

    @Drukpa said: How can that be possible if they are on two different VPS.

    what about use samba for file sharing then vpn for additional securiry?

  • If your so worried about resources with apache why not just set its resource usage lower than it is currently..

  • "If on a same server, then two web servers cannot listen on port 80, can they?"

    nginx port 80
    apache port 8080

    and install libapache2-mod-rpaf

  • @DomainBop or he could run IP1:80 apache, IP2:80 nginx

  • or he could remove apache and just use nginx.

    Yes my solution will be nearly instant.

  • @RyanD said: @DomainBop or he could run IP1:80 apache, IP2:80 nginx

    Looks like I will do this, using two IPs. Thanks.

  • MaouniqueMaounique Host Rep, Veteran

    @Drukpa said: Looks like I will do this, using two IPs. Thanks.

    I dont see how that will decrease load. It would at least need more memory, if you have to have apache for php and whatever else, loading nginx to server static content only seems like a waste as apache already uses the resources and serving the static content too will need less than nginx will need to load and just stay there, IMO.

  • DomainBopDomainBop Member
    edited February 2013

    "he could run IP1:80 apache, IP2:80 nginx"

    He could but it seems like a waste of an IP if he's running them both on the same VPS.

    Digital Ocean has a setup guide for nginx/apache using the different ports method:

    https://www.digitalocean.com/community/articles/how-to-configure-nginx-as-a-front-end-proxy-for-apache

    "I dont see how that will decrease load. It would at least need more memory, if you have to have apache for php and whatever else, loading nginx to server static content only seems like a waste as apache already uses the resources"

    Using nginx as a front end proxy for apache to server static files actually reduces load and memory usage significantly especially if you're running a site with photo galleries. We use nginx front end proxy/apache/php-fcgi(and memcached/apc) on all of our servers/VPS's that have over 512MB memory and when we made the switch from straight apache to nginx front end/apache back end we saw load decreases of up to 50% on some of our servers.

    The Parallels site has a good summary of the benefits of using nginx as a front end reverse proxy for Apache:

    "Such combination of nginx and Apache gives the following advantages:

    The maximum number of concurrent connections to a website increases.
    The consumption of server CPU and memory resources decreases.
    The maximum effect will be achieved for websites with large amount of static content (like photo galleries, video streaming sites, and so on).
    Efficiency of serving visitors with slow connection speed (GPRS, EDGE, 3G, and so on) improves.
    For example, a client with the 10 KB/s connection requests some PHP script, which generates a 100 KB response. If there is no nginx on the server, the response is delivered by Apache. During these 10 seconds required to deliver the response, Apache and PHP continue to consume full system resources for this open connection. If nginx is installed, Apache forwards the response to nginx (the nginx-to-Apache connection is very fast as both of them are located on the same server) and releases system resources. As nginx has lower memory footprint, the overall load on the system decreases. If you have a large number of such slow connections, using of nginx will significantly improve website performance."
    http://download1.parallels.com/Plesk/PP11/11.1/Doc/en-US/online/plesk-administrator-guide/index.htm?fileName=70837.htm

  • mikhomikho Member, Host Rep

    mount a directory on the second vps from the first and when users upload images they will be saved into the mounted dir. (saved on second vps). then you have to create a function to return the static.domain.com address instead your www.domain.com address.

  • 5 letters

    SSHFS

  • SpeedBusSpeedBus Member, Host Rep
    edited February 2013

    rsync the files to the static server and/or mount the directory BUT mounting the directory would well mean that if the main server would be loaded, it would slow down the "static" file serving mirror too.

    and rsync would mean that only the new files would be copied over and not the already existing ones, you would have to make a cron to make rsync copy/update the new files and about once per minute would be okay I think ?

    Hence, best would be to keep both as separate as possible and rsync images to the "static" host and serve them from there.

  • Nginx reverse proxy with a 24 hour cache.
    High popularity images will always be in cache, and low popularity images won't waste unnecessary resources.

  • Is it possible to write a PHP API on the Nginx server that accepts the image? That way, the php file on the apache server will send the file to a php file on nginx which will store the image and return the filename.

    I can install nginx with php-fpm/fastcgi on the nginx web server. It will run no other PHP files, but only that upload (accept file/API).

  • 24khost24khost Member
    edited February 2013

    Okay so we do this with our prestashop installation. You don't need to host your images on a different server as per how nginx works. So we use 4 nginx files.

    Birchtreelane.com is on port 80 and the image directory is let say
    the images are stored in /home/birch/images/

    img.birchtreelane.com:88 root dir /home/birch/images/,
    img.birchtreelane.com:89 root dir /home/birch/images/,
    img.birchtreelane.com:90 root dir /home/birch/images/,

    This gave us our sudo cdn with only one server as it allowed us to load images much faster as they are static.

  • @Drukpa said: How can that be possible if they are on two different VPS.

    Why not just create an A record, that says like network.domain.com is the images/js/css/etc. directory, then in files just link to that in site.

  • @24khost said: Okay so we do this with our prestashop installation. You don't need to host your images on a different server as per how nginx works. So we use 4 nginx files.

    Birchtreelane.com is on port 80 and the image directory is let say

    the images are stored in /home/birch/images/

    img.birchtreelane.com:88 root dir /home/birch/images/,

    img.birchtreelane.com:89 root dir /home/birch/images/,
    img.birchtreelane.com:90 root dir /home/birch/images/,

    This gave us our sudo cdn with only one server as it allowed us to load images much faster as they are static.

    So the images url have a port number in them?

    @curtisg said: Why not just create an A record, that says like network.domain.com is the images/js/css/etc. directory, then in files just link to that in site.

    You mean, create a subdomain like static.domain.com that points to a different ip? and www.domain.com pointing to another ip? And have nginx and apache listen on port 80 on those IPs on the same server, right?

  • 24khost24khost Member
    edited February 2013

    @Drukpa, you can set that up however you want but this worked for us, we just chose to use different ports instead of using multiple different sub domains. could be achieved the same way. just how we chose to do it.

    Allowed it to act as it's own server really, instead of sharing port 80.

  • Simply have two servers, both listen on port 80, just have static.domain.com point to other server, domain.com point to main server.

  • you can do sshfs, glusterFS etc

    or upload directly to your images vps, the upload interface it self @ www.domain.com, but at the back-end, when user upload static content, it actually saved to static.domain.com (just change the upload directory target).

Sign In or Register to comment.