server {
    listen  80 default_server;
    include /etc/nginx/default.d/*.conf;

    # Rewrite the path to static files.
    # root supplies the uptree path the match gets appended to the path specified
    # by the root directive
    location ~* /(\w+)/static/ {
        root /srv/web2py/applications/;
    }

    # alias allows us to completely rewrite the static path
    location ~* ^/(?!ns/)(\w+)/books/(published|draft)/(\w+)/(_static|_images|images)/(.*)$ {
        alias /srv/web2py/applications/$1/books/$3/$2/$3/$4/$5;
    }

    location /ns/ {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header Host $http_host;
      # we don't want nginx trying to do something clever with
      # redirects, we set the Host: header above already.
      proxy_redirect off;
      # Allow web sockets.
      proxy_buffering off;
      # `proxy_pass <https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass>`_: set the protocol and address of a proxied server and an optional URI to which a location should be mapped. **Tricky**: Specifying the ``location`` above with a trailing slash and including a trailing slash at the end of ``proxy_pass`` causes nginx to strip of the ``/ns`` prefix when sending it to gunicorn. Quoting the docs: "If the ``proxy_pass`` directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive."
      proxy_pass http://unix:/run/gunicorn.sock:/;
    }


    location / {
        include uwsgi_params;
        uwsgi_pass unix:/run/uwsgi/web2py.sock;
        uwsgi_param	DBURL	postgresql://runestone:passworda@localhost/runestone;
        uwsgi_param     WEB2PY_CONFIG development;
    }
}
