Configuring URL rewrite rules on Nginx

vps

#1

My purpose in going to a VPS server was so I can configure hosting of an open source web application to serve tiled map images. I’m developing web pages in javascript using leaflet.js and mapbox.js, and using an application called Tilemill to create database files that combine the information needed to display maps I’m creating on QGIS (they happen to be of trout streams).

The maps will be hosted off of a subdomain (http://maps.wildtroutstreams.com) which is currently served off of my VPS.

I’m using an open-source php program called tileserver.php to actually serve the map data. My javascript requests the filenames for resources (like .png images) that are embedded inside the database file. I need to provide some rewrite rules to the web server to capture these URL requests and hand them to tileserver.php instead. This is located in a directory: /server

The developer of the app uses Nginx for his own implementation. He suggests that a standard implementation of this would look like this:

server { 
    listen 80;
    server_name maps.wildtroutstreams.com;

    location ~ ^/server/((?!tileserver).)$ {
        rewrite ^/server/(.*) /server/tileserver.php last;
    }
}

I’ve been studying the wiki for Nginx and wanted someone who knows more than me to confirm my thoughts on how to approach this. Would this go into a .conf file for the domain? The approach I’m planning is the following:

  1. Go to my panel and change the web server to Nginx from Apache

  2. Create the directory:
    mkdir -p /home/myusername/nginx/maps.wildtroutstreams.com/

  3. Create the .conf file:
    nano /home/myusername/nginx/maps.wildtroutstreams.com/tileserver.conf

  4. Inside the .conf file would go the two lines of code: 1) the location rule, 2) the rewrite rule (with one set of curly brackets surrounding the rewrite rule)

Does that seem right?


#2

Update: I went ahead and updated my account along the lines described in my post above. Everything seemed to go smoothly. There’s now a tileserver.conf file in the intended directory which contains the two lines of code.

Per instructions in the Wiki, I told Nginx to restart.

Unfortunately, the redirect code isn’t working. Is there some way to check that tileserver.conf is being loaded?


#3

This is one of the reasons why I reinstalled nginx completely not using dreamhost packages, it’s confusing for long-time users using nginx and other web-packages.

Either way, the port can be left out, as far as I’m concerned that’s not required unless it’s mentioned seeing port 80 is the default listen port. And be sure to include the following line:

inside the *.conf file so the conf files knows where to look. So you get something like this:

[code]server {
server_name maps.wildtroutstreams.com;
root /home/myusername/nginx/maps.wildtroutstreams.com/;

location ~ ^/server/((?!tileserver).)$ {
    rewrite ^/server/(.*) /server/tileserver.php last;
}

}[/code]

I think it explains what root exactly does. I assume that the /server/ redirect that you wish to use, is located in maps.wildtroutstreams.com. If so, then the root path should be adjusted seeing the ‘myusername’ is left empty and it should work. But let me know if it doesn’t.

You can also use sudo nginx -t (if the cmd works for you) to test your configurations. It’ll give an error if something is up, and shows an OK if the config is good.


#4

Hey, Skyrider, thanks for your post.

The trouble turned out to be in the code that I had been given. The developer sent me some different lines, and they now work.

I’m still using the .conf structure that Dreamhost recommends, as documented in the WIKI. Essentially you create a nginx directory under the username (e.g. /home/myusername/nginx/maps.wildtroutstreams.com/). Then, for whatever .conf files it finds, it adds the text they contain to the default server configuration. You don’t need to duplicate the root command, since that’s already in the default server configuration (in fact it gives you an error if you try).

Being a complete Linux novice, I feel more confident keeping this as Dreamhost “standard” as possible. However, one important consideration is, you do need to uncheck the Dreamhost managed box for web servers on the web panel. Otherwise, it ignores the .conf file. The WIKI doesn’t say that, but customer support did.

Thanks to your help and some other folks in this community, I THINK I’m now off to the races. Thank you all!


#5

I wasn’t aware you already had a main *.conf file for the main server ;), hence I mentioned the root. Otherwise I would have mentioned to add a:

code to include in the main domain conf to include a different conf file.


#6

You shouldn’t have to disable management on the web server entirely, but you will need to restart nginx (“sudo /etc/init.d/nginx restart”) to get it to read the new configuration files. I think disabling management may have incidentally triggered a restart, but I’d recommend you switch that back; some features of the panel won’t work properly while management is disabled.


#7

Helpful post. My question at this point is how can I reload the nginx conf file without having to restart each time? The wiki has

but now there’re no sudo privileges.