0

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: static.mysite.com 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
                                text/javascript;

        server {
                  listen                 8484;
                  client_max_body_size   1M;
                  server_name            www.mysite.com mysite.com;
                  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 www.mysite.com/static/...) is mapped to resources available at /home/www-data/mysite/static/.... Similarly, request for www.mysite.com/favicon.ico 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, static.mysite.com/... is mapped to /home/www-data/mysite/static/...?

MLister
  • 211
  • 1
  • 5
  • 12

1 Answers1

1

Use a rewrite rule in the nginx.conf

location ^~ /~sub/ {

rewrite ^/sub/(.*) http://sub.example.com/$1 permanent;

}

Vladimir
  • 46
  • 2