9 ms·
Nginx for Developers: An Introduction
- TallboyOne 13y agoGood article. Be wary of the well documented zero-day exploits if you're using php. This is one of my other favorite articles: http://docs.ngx.cc/en/latest/topics/tutorials/config_pitfalls.html http://docs.ngx.cc/en/latest/topics/tutorials/config_pitfall... Some good introductory stuff here as well: http://pineapple.io/resources/tagged/nginx http://pineapple.io/resources/tagged/nginx
- Nate75Sanders 13y agowary
- TallboyOne 13y agoYikes, I'm supposed to be the grammar and spelling nazi :P
- mikegioia 13y agoThat first article was awesome. I found 2 things in my configs that were security holes :/
- emillon 13y agoHow can a zero-day be well documented? By definition it's not yet public.
- meritt 13y agoIt sounds scarier than saying: "If you use PHP, remember to set the following in your .ini" cgi.fix_pathinfo = 0
- jonknee 13y agoSo not zero day and not an exploit. A terrible idea for a feature though, PHP seems to be rich with those.
- meritt 13y agoNot really a terrible idea at all. They are simply conforming to the CGI specification. In realizing the potential security issues associated with doing so, they offer a way to disable that behavior.
- samsnelling 13y agoCompletely off-topic: Is that a custom WP theme? I love the look and feel of this article. On-topic: Thank you for this. I am going to purchase a DO box this weekend and play around with nginx instead of apache. For some reason when I last tried to use nginx, I could not get location {} to work. Granted it was some time ago. Does anyone have any resources on nginx as a load balancer?
- jenius 13y agoActually it's a very simple custom non-wordpress theme I put together in about an hour, the entire thing built on top of http://roots.cx http://roots.cx dynamic content : ) Another note, the share button at the bottom (just pushed a minute ago so hard refresh if it's cached) is from this plugin we put together and have been enjoying so far: https://github.com/carrot/share-button https://github.com/carrot/share-button Glad you enjoyed the article! Let us know if you run into any trouble, happy to help or add debugging techniques etc
- justincormack 13y agoI can't read the article on my Android phone because I can't resize it to fit on screen. So please remove the theme or fix it...
- jenius 13y agoOh man, I'm sorry! I totally dropped the ball on mobile - I'm going to push a responsive design right now. Sorry Justin! EDIT: Updated, should work great on mobile devices now
- justincormack 13y agoMuch better!
- TallboyOne 13y agoBasic introduction: http://blog.jsdelivr.com/2013/01/nginx-load-balancing-basics.html http://blog.jsdelivr.com/2013/01/nginx-load-balancing-basics...
- thu 13y agoI think the sites-available is a debianism, not present on all distros. One nice thing that nginx supports is include directives with wildcards so you can e.g. have /srv/example.com/nginx.conf and /srv/api.example.com/nginx.conf and include them in the main configuration file with include /srv/*/nginx.conf.
- ChikkaChiChi 13y agoI may be misunderstanding you, but sites-available and the conf files are usually two separate entities. sites-available | sites-enabled is analogous to using a2ensite in Apache without the automagic (you symlink yourself)
- callmeed 13y agoYeah, I run nginx on both Ubuntu and RedHat boxes. On RedHat, the conf files go in /opt/nginx/conf ... there is no sites-avail/enabled
- lowboy 13y agoI ran into a version of Nginx on CentOS had sites-avail/enabled.
- ciclista 13y agoI'm currently running Centos 6 on my VPS and it has this file structure.
- 13y ago
- chuhnk 13y agoFor some strange reason I was expecting an article on nginx module development. http://www.evanmiller.org/nginx-modules-guide.html http://www.evanmiller.org/nginx-modules-guide.html
- merlincorey 13y agoYou aren't at all alone. I was really hoping for updated module development information from someone currently in the trenches.
- warmwaffles 13y agoI am a little hesitant on restarting nginx. When you do that, it drops the connections that are currently attached. Instead, you may want to do `nginx reload`
- mrweasel 13y agoYeah, I disliked that part to. The commands you need to know is: nginx -t (test your configuration) nginx -s reload (reload your configuration) Also I prefer "nginx -s restart".
- jenius 13y agoGreat feedback guys - we'll look this over and probably change it in the article : ) The reason it's as such is because of what @timroman said in his reply, but if this method is no different and scales better no reason not to use it instead.
- warmwaffles 13y agoPossibly want to compare when to use restart and reload, and the drawbacks of both.
- timroman 13y agoI can't imagine there are many connections to worry about if you're configuring a new server.
- warmwaffles 13y agoRight but if you are modifying and existing server, then this needs to be taken into account. We have several servers who are constantly serving connections and we can't have any of them drop otherwise it will result in a poor user experience. I just wish the article talked about using reload as well as restart and when to use them.
- 13y ago
- sergiotapia 13y agoIf you like this article, I wrote a similar one called Devops for Dummies. http://tech.pro/tutorial/1335/devops-for-dummies-vps-configuration-from-scratch-rails-nginx-postgresql http://tech.pro/tutorial/1335/devops-for-dummies-vps-configu... It teaches you how to setup a DigitalOcean (or any other) VPS from scratch to host N amount of Rails applications using Nginx and Passenger. It took me some time to get things working properly and I distilled it into this short article that holds your hand and takes you from A to Z.
- jenius 13y agoAwesome, thanks @sergiotapia!
- mountaineer 13y agoThis is great, I've been on the fence about firing up a VPS or two rather than use Heroku, this may be just what I need to get going.
- gvb 13y agoNice. One step I would add is after restarting ssh and before rebooting while still logged in as root is to start a second ssh session, log in as the new user, and verify you can sudo. Your current instructions are predicated on success. By verifying your new user login and sudo capability before logging out as root (rebooting), you can fix typos or other Bad Things. Not that I would ever make a typo while editing sudoers. ;-)
- dorian-graph 13y agoThank you for linking to your article, it was perfect timing as I'm about to throw up a tiny Ruby/Sinatra/PostgreSQL site I've been making for a local need. The quality of my actual code aside, I understand that servers are incredibly complex beasts, especially when it comes to security, and I want to ask (And because of their complexity, I assume it's a small question with a huge, complex answer), how relatively 'safe' would I be following a guide like yours along with other ones that specify some simple, good practices for setting up a server? By safe, I mean in the context of the server getting hacked. I know people wouldn't bother but I'd rather at least feel a tiny bit safe. I'm going with a VPS (Digital Ocean) too so I can slowly learn all these things.
- DanielBMarkham 13y agoBeen thinking about nginx for some time. If I could get an easy Mono/F# implementation working with it, I'd jump on it in a heartbeat.
- mixmastamyk 13y agoIf it can talk to a socket, nginx can talk to it. Is there something I'm missing?
- deleted 13y ago[deleted]
- stonemetal 13y agoI haven't tried it myself but the mono guys have an article on getting nginx up and running with fastcgi-mono-server2 http://www.mono-project.com/FastCGI_Nginx http://www.mono-project.com/FastCGI_Nginx
- coderguy123 13y agotry this: https://github.com/NancyFx/Nancy/wiki/Hosting-Nancy-with-Nginx-on-Ubuntu https://github.com/NancyFx/Nancy/wiki/Hosting-Nancy-with-Ngi... it worked perfectly for me on digitalocean
- 286c8cb04bda 13y ago> The default port for the internet is 80, so if there’s no port in a url, that means it’s 80. The Internet has no default port; 80 is for HTTP. If the port is omitted, then the default for the scheme[1] is assumed. </pedant> [1]: https://en.wikipedia.org/wiki/URI_scheme https://en.wikipedia.org/wiki/URI_scheme
- ryandotsmith 13y agoIf you are interested in running NGINX on Heroku, checkout my buildpack: https://github.com/ryandotsmith/nginx-buildpack https://github.com/ryandotsmith/nginx-buildpack
- falsedan 13y agoI like this article because it reaffirms the demand for competent devops who've taken an afternoon to read all of http://wiki.nginx.org/DirectiveIndex http://wiki.nginx.org/DirectiveIndex.
- antocv 13y agoWhat makes this "for developers"? Whats the difference if a sysadmin is reading this, or a Linux user? Im surprised to see this on the frontpage, it is less than basics in anything any hacker worth its name can do.
- srhngpr 13y agoI think it should read "Web Developers" - those who may be less experienced in setting up a web server like nginx.
- oneeyedpigeon 13y agoWhy would a Web Developer be less experienced in setting up a Web server than a Developer? - a touchy Web Developer :)
- jollari 13y agoYou're correct, the audience is really just a matter of semantics. The reason we wrote this post, knowing that this is very basic stuff, is that we found that a lot of the tutorials we'd come across didn't cover the introductory process end-to-end. You're just as surprised as we are to see it on the frontpage!
- jenius 13y agoIn my view, you answered your own question with the second sentence. Sysadmins and linux hackers are no doubt familiar with this stuff anyway and used to reading through sysops-style docs and wikis. Many other developers, we assumed, would not be as familiar with ops at first, and would prefer more of a basic introduction that walks through the steps and explains things clearly.
- tszming 13y agoFor starter nginx's configuration, the h5bp nginx config [1] is pretty awesome, e.g. sending correct mime types, protecting hidden files, cross domain webfont, expires, gzip, server tunings all included. [1] https://github.com/h5bp/server-configs/tree/master/nginx https://github.com/h5bp/server-configs/tree/master/nginx
- jenius 13y agoGreat suggestion, thanks @tszming!
- ChikkaChiChi 13y agoThis article is just fine for IDE-side development, but I would not recommend for anyone to use the version of nginx in the standard Ubuntu package manager. It traditionally is several version back missing important key features that you might want to take advantage of.
- nsmartt 13y agoGenerally, it's installed using apt, but from a source maintained by nginx. It's just a quick modification of sources.list. http://nginx.org/en/linux_packages.html#stable http://nginx.org/en/linux_packages.html#stable
- callmeed 13y agoOne of my favorite nginx features I just discovered is that you can use a regex in the server_name and then you can use the capture as a variable later [1] ... this lets you serve domains or subomains from completely different directories without a new conf file or a reload. I'm using it at http://www.utterson.me http://www.utterson.me to serve up static Jekyll blogs on a subdomain. I may be wrong, but I don't think Apache can do this (it can do regex but you don't get a variable). [1] http://nginx.org/en/docs/http/server_names.html#regex_names http://nginx.org/en/docs/http/server_names.html#regex_names
- ericclemmons 13y agoI've done something similar in Apache with mod_vhostalias, where you set a VirtualDocumentRoot that specifies the pattern to match against and the dynamic directory path to serve up.
- GibbyBorn 13y agoGuys, don't link to the community wiki, it contains a lot of obsolete or just wrong information. Always use official documentation: http://nginx.org/en/docs/ http://nginx.org/en/docs/
- lowboy 13y agoI like just keeping a single "etc/nginx/sites" folder, including "sites/*.ON" in my main nginx.conf and then appending .ON to the sites that are enabled: active.site.com.ON another.active.com.ON not.active.com
- dkokelley 13y agoThat's clever. Coming from Apache I was disappointed not to see something equivalent to: a2ensite (site)
- stayclassytally 13y agoWouldn't it be kind of trivial to right a function to would take a directory name as a parameter and symlink it from site-available to site-enabled?
- dkokelley 13y agoIt would be. That's exactly what a2ensite does. That's what I ended up doing.
- tmzt 13y agoYou can also use the Debian-style setup with sites-enabled/*, though I don't know of enable/disable scripts for nginx.
- pilif 13y agoThat's a debian feature. Not an Apache feature.
- mfjordvald 13y agoIt's kinda depressing that this article is basically just a copy of one I wrote in 2010 and it's still something that gets attention as interesting stuff. Nginx documentation obviously still has a long way to go.
- jenius 13y agoHey mfjordvald, We were not aware of your article at all, and if it's "basically a copy" it was entirely coincidental. I think the way you're looking at is very negative though -- any and all efforts to improve understanding of a great and powerful dev tool I think are a positive thing for the community. I'd love to see your article too, could you link to it?
- mfjordvald 13y agoIt's not your article that's depressing, it's the fact that nginx documentation hasn't come further than this over the years. Obviously there's still a need for even basic nginx information and there's absolutely no blame from me for filling that need. If anything I should blame myself for not marketing my own stuff more. :) The article I mentioned can be found here: http://blog.martinfjordvald.com/2010/07/nginx-primer/ http://blog.martinfjordvald.com/2010/07/nginx-primer/
- forgotAgain 13y agoI've actually had your article bookmarked for some time. I got it from the nginx wiki page at http://wiki.nginx.org/Configuration http://wiki.nginx.org/Configuration. I was more surprised than depressed about the upvotes for the current article. It really makes me wonder if the technical level of the audience here hasn't declined substantially. The current article is well written but it's aimed at a "Dummies" rather than developer level.
- zalew 13y agoyeah, you got to checkout the number of twitter bootstrap tutorials.
- 13y ago
- klearvue 13y agoIn the example given "listen 80;" is completely unnecessary since it's a default.
- jenius 13y agoIf you had read carefully, you would notice that it said that right under ; )
- w0ts0n 13y ago" nginx (pronounced engine-x). " I have literally never heard anyone call it that.
- Zikes 13y agoSee http://en.wikipedia.org/wiki/Nginx http://en.wikipedia.org/wiki/Nginx
- diggan 13y agoWell, me, I had never talked or heard anyone talk in "real"-life about nginx before up until one month ago and apparently, it's pronounced that way. Look here: http://wiki.nginx.org/Pronunciation http://wiki.nginx.org/Pronunciation
- jlogsdon 13y agoI have literally never heard it called anything else, even before it started getting traction. I'm curious how you've heard it said before?
- oneeyedpigeon 13y agoHow on earth else would you pronounce it? Now, lighttpd, on the other hand ...
- jlgreco 13y agoI've always pronounced it "en gee eye en ecks". Not the most elegant, but it rolls of the tongue relatively well and nobody who was familiar with nginx has ever been confused.
- seclorum 13y agoI've always wanted to use nginx to build web apps in Lua .. does anyone know/can recommend any great tutorials with this in mind? It'd be nice if I could learn how to use nginx+lua to build a blog, and so on .. I'll use google, but on the off chance someone on HN knows of something already, I'm all ears ..
- merlincorey 13y agoYou might be interested to know that NginX can even embed Lua internally as a scripting language in the configuration[1]. As for your goals of connecting NginX to serve your Lua project, well that's quite simple really. Unlike Apache or Lighttpd, NginX is not an application server. NginX is only a reverse proxying HTTP server that speaks HTTP, FastCGI, and UWSGI. To connect your Lua application to NginX, your Lua application will either need to speak HTTP, FastCGI, or UWSGI. I don't know anything about Lua so I can't really help you figure out which is best, but in terms of setting up with NginX, it mostly just changes which flavor of pass you will use. [1]- http://wiki.nginx.org/HttpLuaModule http://wiki.nginx.org/HttpLuaModule
- seclorum 13y agoThanks for the reply - I am definitely learning a lot from this feedback, and appreciate the effort.
- justincormack 13y agoThere is an intro talk and slides from London Lua here http://www.londonlua.org/scripting_nginx_with_lua/index.html http://www.londonlua.org/scripting_nginx_with_lua/index.html It is pretty easy once you get started...
- seclorum 13y agoGreat - that looks like exactly what I need to get myself educated on the subject - really appreciate the great response, thank you!
- minikomi 13y ago
- deleted 13y ago[deleted]
- raylu 13y agoSlightly off-topic, but I'm sad that people still use apt-get. aptitude is pretty much just better[1]. Also, if you're reading this article you probably don't need the slightly better performance/throughput of nginx. Nginx's configuration is just terrible. You can cause segfaults and all sorts of unexpected behavior with the If directive[2]. And who came up with the idea that rewrite[3] should sometimes redirect instead of rewriting? If the replacement string begins with http:// http:// then the client will be redirected, and any further rewrite directives are terminated. Apache's configuration might be ugly and unwieldy and the community might be horribly infested, but at least the configuration makes sense. Lighttpd has none of the above problems, though. [1] http://superuser.com/questions/93437/ http://superuser.com/questions/93437/ [2] http://wiki.nginx.org/IfIsEvil http://wiki.nginx.org/IfIsEvil [3] http://wiki.nginx.org/HttpRewriteModule#rewrite http://wiki.nginx.org/HttpRewriteModule#rewrite
- Rovanion 13y agoWell Ubuntu doesn't ship aptitude by default any more which would explain parts of it.
- raylu 13y agoAh, that's unfortunate. https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/592336 https://bugs.launchpad.net/ubuntu/+source/ubuntu-meta/+bug/5...
- merlincorey 13y agoI'm extremely biased as I have been using NginX for many years now; however, I've got to completely disagree with you. NginX's configuration is beautiful, elegant, and concise. It kicks the ever living crap out of configuring Apache or Lighttpd (the two other HTTPd's I have extensive experience with). It seems so strange, because it's different. NginX's configuration language is declarative. If is imperative and actually using if inside locations in NginX creates an inner location and executes a mini language to process. 14:42 <+merlincorey> !ifisevil 14:42 < ngxbot> please see above Please see http://wiki.nginx.org/IfIsEvil http://wiki.nginx.org/IfIsEvil for more about that in particular. Finally, I have to disagree with OP... NginX has great documentation, written by Igor himself initially (and edited by the community) available here: http://nginx.org/en/docs/ http://nginx.org/en/docs/
- dergachev 13y agoIf you liked this, consider checking out Mastering Nginx by Dimitri Aivaliotis. I was quite surprised how helpful this book was. http://www.amazon.com/Mastering-Nginx-Dimitri-Aivaliotis/dp/1849517444 http://www.amazon.com/Mastering-Nginx-Dimitri-Aivaliotis/dp/...
- diminoten 13y agoThis is fantastic, thank you! The way it's written is great because it doesn't treat me like an idiot (I get why books do that, but it can grow tiresome). I wish more tech books were written like this. While we're picking nits, I thought this might be worth mentioning: > The default port for the internet is 80 I'd change that to: > The default port for the web is 80 Again, wonderful guide!
- DelvarWorld 13y agoThank you for this. The nginx documentation is the biggest joke of anything ever written.
- saravk 13y agoVery nice article useful for Nginx newbies. While we are on that subject, can anyone please let me know if we can use Nginx for page cache busting purposes. It is easy to configure Nginx to check if the cached version of the requested URL exists and serve it. But i would like to know if the configuration can be extended bit further to serve the cached file only if is less than a few days old?
- geuis 13y agoJust a note on readability. You have a font color of #555 against #fff; Its a light grey against white background and very hard to read.
- matthieupiguet 13y agoNice introductory article. Maybe it should have included a word about SSL support, especially the fact that you need to build nginx with ssl support, it doesn't come out of the box, so make sure you plan for when you'll need it