Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

All I want to do is to set up a nginx server to serve static files ONLY. This nginx server sits behind a HAProxy, and HAProxy directs all the requests for static files to this nginx server.

I have already created a subdomain: record in Linode DNS manager, but I am not sure what to do with the nginx config file exactly, here is what I have now:

wroker_process 1;

events {
        worker_connections  1024;

http {
        include                 mime.types;
        default_type            application/octet-stream;

        sendfile                on;
        keepalive_timeout       60;
        gzip                    on;
        gzip_http_version       1.1;
        gzip_comp_level         3;
        gzip_types              text/plain text/html text/css
                                application/x-javascript text/xml
                                application/xml application/xml+rss

        server {
                  listen                 8484;
                  client_max_body_size   1M;
                  server_name  ;
                  access_log             /home/www-data/logs/nginx_access.log;
                  error_log              /home/www-data/logs/nginx_error.log;

                  location / {
                        index           index.html;

                  location ~* /static/ {
                        autoindex       on;
                        alias           /home/www-data/mysite/static/;
                        access_log      on;

                  location = /favicon.ico {
                        rewrite (.*) /static/favicon.ico;

                  location = /robots.txt {
                        rewrite (.*) /static/robots.txt;

Notice that currently all requests for static files (to is mapped to resources available at /home/www-data/mysite/static/.... Similarly, request for is mapped to /home/www-data/mysite/static/favicon.ico.

How do I change the above to use the subdomain for all static files? For example, is mapped to /home/www-data/mysite/static/...?

share|improve this question
up vote 1 down vote accepted

Use a rewrite rule in the nginx.conf

location ^~ /~sub/ {

rewrite ^/sub/(.*)$1 permanent;


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.