Take the 2-minute tour ×

I have a simple example running ngnix that proxies to my node.js app running on localhost:3001. Now I want to add some optimizations and the problem is I'm not sure I completely understand the way ngnix config files work.

What I want to do is to serve index.html, about.html and main.js from the CDN via a proxy-forward through ngnix. I imagine I need to add something like a rewrite just for those two files (and an entire images and css directory eventually)

So user goes to mydomain.com .. ngnix kicks in and delivers index.html from cdn.mydomain.com/index.html.

Here is what I have now:


proxy_redirect              off;                                                                                                                                                    
proxy_set_header            Host $host;                                                                                                                                             
proxy_set_header            X-Real-IP $remote_addr;                                                                                                                                 
proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                             

client_max_body_size        10m;                                                                                                                                                    
client_body_buffer_size     128k;                                                                                                                                                   
proxy_connect_timeout       600;                                                                                                                                                    
proxy_send_timeout          600;                                                                                                                                                    
proxy_read_timeout          600;                                                                                                                                                    
proxy_buffer_size           4k;                                                                                                                                                     
proxy_buffers               4 32k;                                                                                                                                                  
proxy_busy_buffers_size     64k;                                                                                                                                                    
proxy_temp_file_write_size  64k;                                                                                                                                                    
send_timeout                600;                                                                                                                                                    
proxy_buffering             off;                                                                                                                                                                                                                                                                                                                                        
# the IP(s) on which your node server is running i choose the port 3001                                                                                                             
upstream app_yourdomian {                                                                                                                                                               
# the nginx server instance                                                                                                                                                         
server {                                                                                                                                                                                
server_name ec2-75-101-203-200.compute-1.amazonaws.com ec2-75-101-203-200.compute-1.amazonaws;                                                                                      
access_log /var/log/nginx/yourdomain.log;                                                                                                                                                                                                                                                                                                                               
# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options                                                              
location / {                                                                                                                                                                          
proxy_set_header X-Real-IP $remote_addr;                                                                                                                                            
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                                        
proxy_set_header Host $http_host;                                                                                                                                                   
proxy_set_header X-NginX-Proxy true;                                                                                                                                                                                                                                                                                                                                    
proxy_pass http://localhost:3001;                                                                                                                                                   
proxy_redirect off;                                                                                                                                                               




share|improve this question
Accessing a CDN through a reverse proxy completely defeats the purpose of using one.... –  ThiefMaster Sep 12 '11 at 8:35
add comment

1 Answer

If you really need to proxy (not redirect to) index, about and main.js, then it would be something like having three more simple locations for each of the above

location = /index.html { proxy_pass ... }

You might also want to take a look at http://wiki.nginx.org/HttpCoreModule#location

For locations without regex the most specific match is used.

Feel free to ask more in the mailing list http://mailman.nginx.org/mailman/listinfo/nginx

share|improve this answer
add comment

Your Answer


