Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Right now, I redirected all the HTTP urls to HTTPS using this code in my nginx config:

server {
  server_name domain.com
  listen 80;

  location /asset/ {
    # make files under asset directory (recursively) be available over HTTP too
  }

  location / {
    rewrite ^ https://$server_name$request_uri? permanent;
  }
}

How can I make files under asset directory (recursively) be available over HTTP too?

share|improve this question

2 Answers 2

up vote 2 down vote accepted
server {
    listen 80;
    server_name domain.com;
    root /var/www;

    location / {
        location /asset {
            try_files $uri =404;
        }
        return 301 https://$server_name$request_uri;
    }
}

If you’d like to pass missing files to your app you could do something like:

location / {
    location /asset {
        try_files $uri @app;
    }
    return 301 https://$server_name$request_uri;
}

location @app {
    # Add whatever logic you'd like to perform, e.g.:
    include fastcgi_params;
    fastcgi_pass /var/run/fastcgi.sock;
}
share|improve this answer
    
Added it, throws 404 on files under asset dir –  Ashfame Jun 24 '13 at 20:20
    
Yes, if desired (you could also go somewhere else if the file does not exist). Plus it is simply the proper way to create locations (nesting) plus return doesn't rewrite, it simply returns (think of rewrite like of a function which is altering a variable by reference and return is simply return). –  Fleshgrinder Jun 24 '13 at 20:22
    
I meant its throwing 404 on a file which exists. The same file is accessible on HTTPS. –  Ashfame Jun 24 '13 at 20:26
    
Have you changed the root directive? Or simply copied all of it? –  Fleshgrinder Jun 24 '13 at 20:27
1  
Ah yes, I didn't have the root directive in this server block. Added it and works now. Thanks! :) –  Ashfame Jun 24 '13 at 20:31

Assuming you need /asset to be accessible over HTTP and HTTPS at the same time,

You need something like this:

# HTTPS server {} section
# HTTP server {} section 
location !~ ^/asset {
   rewrite (.*) https://$server_name$1 permanent;
}
share|improve this answer
    
Makes sense. Is there a syntax error in it? Caused nginx reload to fail. –  Ashfame Jun 24 '13 at 20:21
2  
Always execute nginx -t before trying to reload or restart, it will give you an error message describing the problem. –  Fleshgrinder Jun 24 '13 at 20:29

Your Answer

 
discard

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.