News for hackers. Stuff that matters.
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @11:01AM (#7640692)
I figure this is as good of place as any to ask my question: Where can I find someone to hire that is able to write Nginx cofigs well. I have spent literally 40ish hours trying to create a Nginx conf that holds up to my OCD. I have been told numerous times on IRC that I am too picky and clean urls are a challenge to write. I am college student and System Administration isn't even my job! Help!
Re:Nginx 1.6.0 stable released by cheald on Thursday April 24, 2014 @11:04AM (#7640717)
I'm pretty sure the best way to accomplish this is to jump into IRC and declare that it is impossible to be done.

You will have 6 answers in 3 minutes.

Re:Nginx 1.6.0 stable released by gaoshan on Thursday April 24, 2014 @11:29AM (#7640873)
Or say, "Well, this is the only way to do it... nothing else works for nginx." and then present an inefficient solution. You will get ripped but solutions will arrive.
Re:Nginx 1.6.0 stable released by faster on Thursday April 24, 2014 @03:50PM (#7642541)
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @11:16AM (#7640794)
I wish, I have tried multiple times and no one wants/can do it.
Re:Nginx 1.6.0 stable released by DrJ on Thursday April 24, 2014 @01:33PM (#7641675)
this is literally happening in this thread
Re:Nginx 1.6.0 stable released by ShaneOG on Thursday April 24, 2014 @11:06AM (#7640730)
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @11:21AM (#7640829)
Yes I have, however I always find it very challenging to modify it to support clean urls and PHP-FPM in an OCD fashion.
Re:Nginx 1.6.0 stable released by w-ll on Thursday April 24, 2014 @11:55PM (#7644693)
Have you tried handling your urls in php? Most apps/frameworks I've worked on in the past few years all do better url mgmt then you are probably going to get with just nginx configs.
Re:Nginx 1.6.0 stable released by leeoniya on Thursday April 24, 2014 @11:39AM (#7640943)
Re:Nginx 1.6.0 stable released by eggnet on Thursday April 24, 2014 @11:49AM (#7641017)
You didn't really specify what you meant by clean URLs but, maybe this will be helpful to you or someone else.

I have a static site served via nginx, but I don't like seeing .html in the URLs.

It's a little tricky serving a static site without the .html extension because you may have a directory and an html page with the same name.

The way to deal with that is to actually use the .html extension in the file system but not URLs.

  location / {
    if ($uri ~ ^/google) {
      break;
    }
    if ($uri ~ ^/y_key_) {
      break;
    }
    if ($uri ~ \.html$) {
      return 404;
    }
    if ($uri = /index) {
      return 404;
    }
    if (!-f $request_filename) {
      rewrite ^/$ /index.html break;
      rewrite .* $uri.html break;
    }
  }
Re:Nginx 1.6.0 stable released by Spittie on Thursday April 24, 2014 @12:42PM (#7641358)
By the way, the nginx wiki says that you should avoid ifs when possible: http://wiki.nginx.org/IfIsEvil

Wouldn't something like this accomplish the same thing? (Sorry if I'm totally wrong, I'm not really good)

   location /google/ {

   }
   location /y_key_/ {

   }
   location ~ \.html$ {
      return 404;
   }
   location = /index {
      return 404;
   }
   location / {
      try_files $uri @rewrite
   }
   location @rewrite {
      rewrite ^/$ /index.html break;
      rewrite .* $uri.html break;
   }
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @12:45PM (#7641385)
This is yet another concern of mine. Most of my solutions involve at least 3 if statements. But I need them in order to check if a requested url is a directory or a file and such.
Re:Nginx 1.6.0 stable released by sondr3 on Thursday April 24, 2014 @10:35PM (#7644443)
What kind of app are you running that requires you to do something like this for clean URLs?
Re:Nginx 1.6.0 stable released by eggnet on Thursday April 24, 2014 @01:20PM (#7641593)
Thanks, I will try this, it looks like it will work.
Re:Nginx 1.6.0 stable released by eggnet on Friday April 25, 2014 @02:00PM (#7648055)
It does work, thanks!
Re:Nginx 1.6.0 stable released by manacit on Thursday April 24, 2014 @12:47PM (#7641396)
I don't mean to be blunt, but using if is not the way: http://wiki.nginx.org/IfIsEvil

Writing clean URLs is much easier with try_files, which allows you to do something like

    try_files $uri.html
If you don't want to see file extensions.
Re:Nginx 1.6.0 stable released by jafaku on Thursday April 24, 2014 @11:41AM (#7640963)
You should let your web framework of choice do that. You should be able to find any directives you need to add to your nginx conf in your framework's docs. Typically they will use a Front Controller (eg: app.php or index.php), so you need a directive to map all the dynamic traffic (ie. usually not images or other static files) to that file. Everything else is done by the framework.
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @12:31PM (#7641281)
I am designing this Ngnix conf for use on small vanilla sites and static generated sites like Jekyll. (Yes I know Jekyll puts all blog posts in separate folders to solve the URL issue, but I don't want that).
Re:Nginx 1.6.0 stable released by nilsbunger on Thursday April 24, 2014 @11:54AM (#7641054)
Try odesk? If you have a clear spec for what you want you can probably get a fixed-price bid.
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @12:39PM (#7641335)
Even the word "odesk" leaves a bad taste in my mouth. I have heard a lot of bad things about these kinds of freelance sites.
Re:Nginx 1.6.0 stable released by nilsbunger on Thursday April 24, 2014 @01:37PM (#7641693)
Have you used them? Like anything, odesk/elance/etc require an investment on your side to make successful.

We've found good people through odesk to take on tasks we weren't good at -- front-end javascript, document translation, audio transcription, to name a few.

Re:Nginx 1.6.0 stable released by michaelbuckbee on Thursday April 24, 2014 @11:20AM (#7640827)
Ask on StackOverflow or ServerFault, offer a bounty if no one answers within a day.
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @11:26AM (#7640855)
not a bad idea. Does either have an official way to offer a bounty? If not, any sites that do? Also, what would you say is a fair price range for this kind of request?
Re:Nginx 1.6.0 stable released by lazyant on Thursday April 24, 2014 @11:31AM (#7640886)
A bounty in those sites is not monetary but you use "karma" points that you win by answering questions etc.
Re:Nginx 1.6.0 stable released by scragg on Thursday April 24, 2014 @12:16PM (#7641198)
I bet it will get closed for being too localized.
Re:Nginx 1.6.0 stable released by mrweasel on Thursday April 24, 2014 @11:48AM (#7641010)
You could pay the Nginx guys to help I suppose.
Re:Nginx 1.6.0 stable released by odonnellryan on Thursday April 24, 2014 @11:38AM (#7640941)
What problems exactly with "clean URLs" are you running into?
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @11:50AM (#7641024)
It is a lot to get into now, but here is a taste.

foo.com/index > foo.com foo.com/ > foo.com foo.com/folder/index > foo.com/folder/ foo.com/bar.html > foo.com/bar foo.com/bar.htm > foo.com/bar foo.com/bar.php > foo.com/bar www.foo.com/ANY OF THE ABOVE TESTS > foo.com/*

other rules: Never add trailing slash unless it is an index file of a directory. Order: .PHP,.HTML,.HTM. Use h5bp/server-configs-nginx as a base. PHP-FPM should be fully supported and not exploitable by the foo.com/random.gif.php bug. 403 and 404 send to /404.html.

I am sure I am forgetting something, but that is a start.

Re:Nginx 1.6.0 stable released by richardjs on Thursday April 24, 2014 @12:22PM (#7641232)
Your specs look a lot like ours (we have a CMS that adds a trailing /index to every page that is part of the core navigation, and we don't want that). Here's the three primary rewrite rules we use for the issue; they don't match your spec exactly, but they might help you get started:

    # - Remove trailing slashes (except root /)
    # e.g. /foo/bar/ -> /foo/bar
    # ([^^] matches every character but the start of the string)
    rewrite [^^](.*)/$ /$1 permanent;
     
    # - Remove .html and .htm extensions
    # e.g. /foo/bar.htm -> /foo/bar
    rewrite ^(.*)\.html?$ $1 permanent;
     
    # - Remove index file URLs
    # e.g. /foo/bar/index -> /foo/bar/
    # (the trailing slash is then removed by the first rewrite)
    rewrite ^(.*/)index$ $1 permanent;
Edit: There's a couple extra directives that go along with the above to make it work that I neglected to include:

    # Try the request URI, and a potential index file in the URI (in the case of a directory).
    # This lets you hit the file WEBROOT/foo/bar/index with the URI /foo/bar,
    # and hit the file WEBROOT/foo/bar.html with the URI /foo/bar
    try_files $uri $uri/index $uri.html =404;

    # You need this (or some other way to provide the type information)
    # if you don't have extensions on your files.
    default_type text/html;
Also, for error pages, you can probably just use the error_page directive:

    error_page 404 403 /404; # The .html in your spec will be stripped off by the above rewrite rule
Re:Nginx 1.6.0 stable released by jcampbell1 on Thursday April 24, 2014 @01:23PM (#7641611)
I have had problems using rewrite in this way. The URL that rewrite analyses may have already been changed by `index` or some other command, and this will lead to redirect loops. To avoid that problem, I have used:

    if ($request_uri ~* "^(.*)\.html?$") {
        return 301 $1;
    }
The above is a safe use of "if", and can be helpful since it operates on the actual uri, not the internal uri.
Re:Nginx 1.6.0 stable released by richardjs on Thursday April 24, 2014 @02:24PM (#7641970)
That's a good suggestion. Yeah, index directives will cause a redirect loop in my above configuration. The try_files directive is effectively taking the place of an index.
Re:Nginx 1.6.0 stable released by saurik on Thursday April 24, 2014 @12:29PM (#7641269)
foo.com/* -> www.foo.com/* seems "cleaner" to me than your opposite, due to the issues with cookie leaking, CDN hosting, "normal user" expectations, etc. Like, please appreciate that your usage of "cleaner" is at best subjective ;P. (And I don't quite understand how you are intending to do "foo.com/ -> foo.com"... all URLs must have a path: you can't just GET, you have to GET /.)
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @12:37PM (#7641321)
I understand the redirecting to non-www is subjective, however I find less characters in the URL is cleaner. Also for a majority of my projects cookies are not used, so it is not a big deal to use a non-www base url.(Static blogs and simple vanilla sites for various school projects, etc).

Also as far as foo.com/ -> foo.com, I was referring to the URL, not the acutal path.

example: https://www.google.com/ redirects to https://www.google.com correct? Maybe I am missing something...

Re:Nginx 1.6.0 stable released by neckro23 on Friday April 25, 2014 @10:01AM (#7646480)
No, that's just your browser (annoyingly) removing the trailing slash from URL shown in the address bar.

http://www.foo.com/ is the "correct" URL. Remember how HTTP works -- you connect to foo.com port 80, then "GET / HTTP/1.1". You can't just omit the "/" and expect it to work.

HTTP clients will just request "/" if no path is specified, so nginx will never even see a request that matches "^foo.com$". You'll cause an infinite redirect loop if you try to force the issue.

Re:Nginx 1.6.0 stable released by joevandyk on Thursday April 24, 2014 @01:05PM (#7641505)
No, it doesn't redirect.

    $ curl -I https://www.google.com/
    HTTP/1.1 200 OK
Re:Nginx 1.6.0 stable released by odonnellryan on Friday April 25, 2014 @07:46AM (#7645824)
I think that's just your browser removing the trailing slash.
Re:Nginx 1.6.0 stable released by marshallford on Friday April 25, 2014 @11:09AM (#7646977)
oh right, that makes more sense.
Re:Nginx 1.6.0 stable released by _RPM on Thursday April 24, 2014 @12:53PM (#7641434)
What you need to do is route EVERY request to your 'index.php` or equivalent, show errors from there. It would look like this: ^ index.php
Re:Nginx 1.6.0 stable released by marshallford on Thursday April 24, 2014 @01:01PM (#7641482)
I am designing this Ngnix conf for use on small vanilla sites and static generated sites like Jekyll. Otherwise, yes I would rely on my CMS's index file.