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

As an example, I'd like to serve , where anyUserName is a any combination of characters apart from path separators, from the /webapps/client directory. So, a query to and a query to will be served from the same directory. I should be able to request files such as or without issue. Basically, I want to drop the username part of the url and serve from the same directory but I need to maintain the username in the browser's url.

I've tried many different config tweaks from a regex in the location to this monstrosity but I in every case I get a 404, a 500 which the logs say is a rewrite or internal redirection cycle, a 200 on every resource but each one is the index, or a 200 on every resource but each except the index is a 404.

    location /public  {
            index collmanagerclient.html;
            alias /webapps/client;
            autoindex on;
            try_files $2/$3/$4/$5/$6 $2/$3/$4/$5/$6/ =404;

This is my nginx configuration:

server {
listen 80;
rewrite ^(.*) https://$server_name$1 permanent;

server {
listen 443;

ssl on;
ssl_certificate /etc/nginx/ssl/ssl-unified.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ecdh_curve secp521r1;
root /webapps/client;

index collmanagerclient.html;
location = / {
    try_files $uri $uri/ =404;

location ~ /public/(?:[^/]+)/(?<filename>.+) {
 alias /webapps/client;

 rewrite ^ $filename last;

location /signup {
    index collmanagersignup.html;
    alias /webapps/signup;
    try_files $uri $uri/ =404;
location /api {
    #turn gzip off to avoid breaking the cpu

    gzip off;

    proxy_pass http://localhost:7801;
    proxy_buffering off;

    include /etc/nginx/proxy.conf;


Thanks, I've been driven insane over the last little while by this.

share|improve this question
It is unclear to me, what exactly is your question. I understood your question the same way as Gmck, that is, you want to serve the same resource to everyone. Please give a concrete example, what URLs you want to use, and what files they should serve from what directories. – Tero Kilkanen Aug 1 '14 at 15:20
I changed to a concrete example rather than the previous poor one. – Everlag Aug 1 '14 at 19:28

I would think that you need to look into the rewrite module. Nginx Rewrite module

Something like:

location /public { rewrite ^ /Foo last; }

share|improve this answer
That results in the scenario I mentioned where I have everything as a 200 but I always have the same resource being returned. I need to essentially turn /public/* into / what I serve on root and that method only lets me serve one item for everything. – Everlag Jul 31 '14 at 2:48

Maybe this one does what you want:

location ~ ^/public/(?:[^/]+)/(?<filename>.+)/?$ {
    alias /webapps/client;

    rewrite ^ $filename last;

And then, use this to replace location = / block:

location / {
    try_files $uri $uri/ =404;

Here we use regular expressions to first match URIs like /public/username/file_to_get.html.

The first part between / characters captures the username, and stores it nowhere (?: modifier).

The second part captures the actual wanted filename to $filename variable.

Then we simply rewrite the request URI to the captured filename.

share|improve this answer
That's exactly what I was looking for but for some reason it doesn't work. I get an error of '2014/08/02 13:32:02 [error] 2528#0: *1335 open() "/webapps/client/public/Foo" f$' when querying /public/Foo which makes it seem like nginx is ignoring alias. – Everlag Aug 2 '14 at 18:31
How does your complete nginx configuration look? There could be some other directives that take precedence to this block. – Tero Kilkanen Aug 3 '14 at 1:54
I added my config; I thought that probably wouldn't be an issue but then again, I'm a novice. Thanks very much for the help! – Everlag Aug 3 '14 at 4:55
Please try the new version in my answer, I added start and end matching to the location directive. – Tero Kilkanen Aug 4 '14 at 12:10
/webapps/client/public/Everlag/collmanagerclient.html is not found (2: No such file or directory) is the new error when the url is (Notice the trailing slash) and the same error as before is still there for the non-slash terminated url. – Everlag Aug 4 '14 at 16:18

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.