Welcome! Log In Create A New Profile

Advanced

How & Where to place/deal with static files in Nginx and Apache set up

Posted by jamesdkirk 
How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 03:21PM
Having decided that I'm likely to build the new web server box using what sounds like a champion set up of Nginx serving static files in front of Apache, which will serve the php files (specifically to run an installation of WordPress Multi User + BuddyPress), I have a simple question that I cannot seem to find a simple answer for (they are all simple solutions, yes?!)

Once everything is set and ready (and there seems to be more than enough info available on how to do this, so I'm not overly concerned) what is the process for uploading and dealing with those static files? For instance, if I'm using my WP install won't static images and other files that are uploaded reside in the WP directory?

Is it simply the case that no matter where the actual ".gif" or ".png", etc. file is makes no difference? Does Nginx simply "know" that the file is being asked for during the http request and it reaches into the file tree and serves it out, while Apache would deal with the dynamic nature of the WordPress pages?

Forgive me if this is just one of those "you should really know the answer to this question" kind of questions, but I'm simply trying to better understand the entire picture before going down the road of setting up the new box with these apps.

And thanks to anyone weighing in. Much appreciated!

~~~~~~~~~~
James D Kirk
jamesdkirk.com
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 05:56PM
Yeah, nginx is pretty smart. You tell it what the document root is, you tell it what requests to pass by proxy to Apache (or to php-cgi if you use fastcgi_pass) - in this case anything ending with ".php" and it will serve the static files.

So in your nginx.conf you might have something like this:

[code]
server {
server_name your-mu-server.com www.your-mu-server.com;
listen your.ip.add.ress:80;
root /path/to/wordpress_mu;

location / {
wordpress_mu_stuff here;
}

#pass php requests to Apache listening on 127.0.0.1:80
location ~ \.php$ {
proxy_pass http://127.0.0.1:80;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
server_name your-buddypress-server.com www.your-press-server.com;
listen your.ip.add.ress:80;
root /path/to/buddypress

location / {
buddypress_stuff here;
}

#pass php requests to Apache listening on 127.0.0.1:80
location ~ \.php$ {
proxy_pass http://127.0.0.1:80;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[/code]


That's assuming they are at separate paths/subdomains. If they are both on the same tld but in different subdirectories then you would use one server block and two separate location blocks. You'll want to install [url=http://stderr.net/apache/rpaf/]mod_rpaf[/url] into your Apache so you will get the actual IP addresses of your visitors (useful if you need to ban an IP).

If you want to use fastcgi (the better way IMMHO) then Google php-fpm and compile your php to include it.

Then you would use the following block instead:

[code]
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /path/to/fastcgi_params;
}
[/code]

A default fastcgi_params file is created when you compile nginx.

My fastcgi_params looks like:

[code]
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
[/code]

--
Jim Ohlstein
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 06:46PM
Very coolness. BuddyPress actually behaves like a set of super plugins within the WPMU install, so there's no need to have a second set of directives just for it.

A couple of follow up questions, if I may?
[list=1]
[*] I'm sure I've come across subdomain wild cards in the nginx config, but not recalling them. They would simply be listed in the config file:
[code]
server_name your-mu-server.com *.your-mu-server.com;
[/code]
Like in Apache virtual host file setup?

[*] Wondering what an example of entry that would go into the:
[code]
location / {
wordpress_mu_stuff here;
}
[/code]
Are we talking file structure or something that my bleary eyes and mind are missing at the moment?!
[/list]

Not heard a ton of "pluses" to using fastcgi so would be curious why you humbly favor it!

TIA.

~~~~~~~~~~
James D Kirk
jamesdkirk.com
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 07:15PM
I'm assuming there might be rewrites or cache settings or something along those lines in that block. Maybe not? I don't know that app well enough to say.

Fastcgi vs Apache biggest advantages would be memory consumption and (probably) CPU usage. Ridding your server of Apache will get rid of all those threads/processes that Apache spawns.Each time a process is spawned CPU time is used.Nginx has native fastcgi support whereas currently connections to a proxied backend use HTTP 1.0 (non-persistent) connections. You can always set it up on a test server and run some benchmarks both ways and see which consumes fewer system resources.

--
Jim Ohlstein
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 07:50PM
Okay, groovin' on the concept of no Apache and thus better CPU usage and persistent connections. I know enough to know that makes a lot of sense and likely a big difference when and if ones server starts getting loaded down.

The question you've raised with me now goes back to the stuff to enter into the "location" block. Are things like rewrites and cache settings the same in this section of nginx as they would be in a virtual host or .htaccess file on Apache? Meaning, is the regex still regex or is there some sort of magical mysterious conversion that will have to be done in order to use the Apache stuff at the nginx level?

Also, if an application or script required rewrite regex in the .htaccess (or even virtual conf) levels on Apache, would/could they stay there in the Apache set up and function properly, or would they be required to be moved to the nginx front server level?

Do you have or can you recommend a quality tutorial for installation of nginx with fastcgi?

As always, thanks for sharing with me and others that will read this at some point!

~~~~~~~~~~
James D Kirk
jamesdkirk.com



Edited 1 time(s). Last edit at 06/10/2009 07:59PM by jamesdkirk.
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 08:17PM
The rewrites will have to be done nginx style. They are only slightly different than Apache rewrites. They can be entered in either a "server" or "location" block, and can even be within an "if" statement. If you only have one virtual host on the server then putting all of the configuration in your nginx.conf makes sense. With multiple virtual hosts, an entry like
[code]
include /usr/local/nginx/sites-enabled/*;
[/code]
in your nginx.conf makes sense. Then you can have a separate file for each virtual host. Adding and removing a virtual host then is as easy as adding or removing a file from that directory and reloading nginx (yes, configuration can be reloaded without restarting and dropping any requests).

There is an English wiki at http://wiki.nginx.org which can help with a lot of this. It has excellent links as well. You might find http://lumanau.web.id/2008/11/27/nginx-rewrite-rules-for-wordpressmu-with-wp-super-cache-plugin-enabled.html useful as well.

--
Jim Ohlstein
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 10, 2009 08:58PM
[b]Ahhhhhh[/b] Sweet, sweet, sweet. Of course, my 7th grade math teacher would never let us use the calculator until we could prove we could do the calculation without it. But in this case, I'm making the exception. His post indicates it's working successfully for his sites, so I'm thinking now I'll be going for the nginx front end with php-fastcgi to process the WordPress Multi User scripting and WP-Super-Cache.

I was actually just reading through this post by Frank "[url=http://tech.nocr.at/tech/how-to-speed-up-wordpress-with-nginx-and-wp-super-cache/]How To Speed Up Wordpress With Nginx And WP Super Cache[/url]" which includes the regex conversion code from your link, along with some nice, verbose commenting and instruction on setting it all up. I'll just have to compare the two, as one states it's for setting up WPMU and the other just WP (Single User).

But looking good. More tomorrow.

Thanks again, Jim.

~~~~~~~~~~
James D Kirk
jamesdkirk.com



Edited 1 time(s). Last edit at 06/10/2009 09:00PM by jamesdkirk.
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 14, 2009 04:12AM
Hey Jim,
Back again! It seems that certain things are working on my slice which is currently serving out [url=http://inverdevalley.com]InVerdeValley.com[/url]

As you can tell most of the images are not showing up; neither the css or js files. And I did set up (successfully, I think) php to be proxy'd through to fastcgi. As such, I'm running WordPress Multi User with BuddyPress, and that's likely where the content showing up in the browser is coming from.

Please let me know what I can post up here to help you help me figure out where those darned static files are at, and why they're not showing up!

Thanks so much!

~~~~~~~~~~
James D Kirk
jamesdkirk.com
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 14, 2009 05:51AM
Okay, I believe I have it figured out, Jim.

I inserted some nginx code for static files, and the css and js files started showing up. Still no images. I verified the images were indeed on the server (even manually downloading to ensure there was nothing wrong with them!) Then I started doing some searches that might show something amiss with WPMU not rewriting URL's correctly.

My hunch proved out in that I was not getting the proper rewrite from the code I had inserted in my vhost to show the static files with nginx. Following is what I've come up with (mostly with help of xazax: [url=http://wpmu.org/nginx-wordpress-26/]Nginx and WordPress ยต 2.6[/url]. Careful on copy and paste however as you'll get line breaks in those lists of file types in his post. nginx no likey!)

[code]
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{root /path/to/your/wpmu/or/wp?/directory/;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$
/wp-content/blogs.php?file=$1 last;
expires 30d;
break;}
[/code]

With no core code hacks or any other changes to my WPMU install, simply adding in the above rewrite code immediately started showing the images again on my site.

Now my (final?) question for you is how am I "sure" that the static files are being served by nginx? Is it simply a matter that I am not running Apache or lighttpd therefore they have to be served via nginx?

Thanks so much for you help, Jim. Much appreciated.

~~~~~~~~~~
James D Kirk
jamesdkirk.com
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 14, 2009 01:48PM
LOL. Seems you helped yourself. :)

If there is no other web server software running then it is nginx serving the static files. If you want to convince yourself further, find an image on your site -- I used http://inverdevalley.com/files/2009/06/mini21.jpg. Then go to http://www.rexswain.com/httpview.html and paste the URL. It will show the headers and that the server is nginx 06.35 (and a few other things about your system).

--
Jim Ohlstein
Re: How & Where to place/deal with static files in Nginx and Apache set up
June 14, 2009 04:03PM
Well, I promise you I could not have gotten as far with this new slice set up without the help of yourself and these forums. Once I've completed transfer from the old slice to the new, I'll be posting up a series on my merry adventures, and be sure to send out plenty of link love over this way.

I'll not be saying goodbye, however, as there's tons more to learn and do, and the Nginx Forums are a great place to get you there.

[b]Thanks again, Jim. Much appreciated![/b]

~~~~~~~~~~
James D Kirk
jamesdkirk.com



Edited 1 time(s). Last edit at 06/14/2009 04:03PM by jamesdkirk.
Re: How & Where to place/deal with static files in Nginx and Apache set up
October 05, 2009 04:02AM
Hello Everyone,

Is there a way i can serve static files from another server? i mean server 1 has nginx and server 2 has nginx also. How should be the configuration of nginx for both servers? Can you please give me an example of configurations that i should use?

Please help me.

Thank you so much...

--
Bernard



Edited 2 time(s). Last edit at 10/05/2009 04:04AM by bernard.barbosa.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 182
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready