django app static files on the different server not get loaded

ningja nginx-forum at forum.nginx.org
Fri Dec 31 22:45:07 UTC 2021


I have two server test1.com and test2.com. test1 is internet public face
server. Test2 is intranet only server. Both servers have nginx docker
running. 
Test1 run a Django app1 which has static files under /app/public/static.
App1 can load the static files and run correctly from URL
https://test1.com/app1. Test2 has a Django app2 which has static files under
/app/public/static on server test2. From URL https://test2.com/app2. Every
thing works include static files. 
The issue is I need config nginx1 to allow people to access app2 from
internet.(public) 

 With the configuration nginx1 blow I can load the app2 but not the app2
static files. The error is :
"GET /static/img/logo-2.jpg HTTP/1.1", host: "test1.com", referrer:
https://test1.com/app2/ . The nginx is looking for app2 static file under
test1 which obviously ‘file not found’. 
How can config nginx1 to looking for app2 static file under test2.com
https://test2.com/app2/?

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
}

http {
  	include       /etc/nginx/mime.types;
  	default_type  application/octet-stream;

  	client_max_body_size 50m;

  	log_format  main  '$remote_addr - $remote_user [$time_local] "$request"
'
  	'$status $body_bytes_sent "$http_referer" '
  	'"$http_user_agent" "$http_x_forwarded_for"';

  	access_log  /var/log/nginx/access.log  main;
  	error_log   /var/log/nginx/error.log;

  	sendfile        on;
  	keepalive_timeout  65;
   
  	map $http_upgrade $connection_upgrade {
     	 default upgrade;
      	''      close;
  	}

  	upstream app {
    	server django:5000;
  	}

 	 server {
    		listen 443 ssl;
    		server_name test1.com;
    		charset utf-8;  
    		root    /app/public;

    		# static files
    		location / {         
      		try_files $uri @proxy_to_app ;      
    		}

    		# prevent XSS, clickjacking, never cache
    		add_header "X-Frame-Options" "SAMEORIGIN";
    		add_header "X-Content-Type-Options" "nosniff";
    		add_header "X-XSS-Protection" "1; mode=block";
    		add_header "Pragma" "no-Cache";
    		add_header "Cache-Control" "no-Store,no-Cache";

    		# app1 static
    		location /static/ {
      			expires 1d;
      			access_log off;
      			add_header "Cache-Control" "public";
      			add_header "Pragma" "public";
    		}   	
   
     		#app2                                             
    		location /app2/ {                                    
      			proxy_pass https://test2.com:444;
      			proxy_set_header Upgrade $http_upgrade;
      			proxy_set_header Connection $connection_upgrade;
      			proxy_redirect off;                                          
   		 }  
 
    		# django
    			location @proxy_to_app {
      			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      			proxy_set_header X-Forwarded-Proto $scheme;
      			proxy_set_header Host $http_host;
      			proxy_redirect off;
      			proxy_pass http://app;
    		}
  	}
     	//end of test1 server

  	#test2 server
  	server{
  		listen 444;
  		server_name https://test2.com;
 		 root    /app/public;

    		# static files
    		location / {           
      			try_files $uri @proxy_to_app ;      
    		}     

    		# except anything in static
    		location /static/ {
      			expires 1d;
      			access_log off;
     		 	add_header "Cache-Control" "public";
      			add_header "Pragma" "public";
    		}
  	}#end of test2
    
  	# redirect http to https
 	 server {
    		listen 80;
    		server_name test1.com;
    		return 301 https://test1.com$request_uri;
  	}

  	# only valid HTTP_HOST header should be used
  	server {
    		listen 80 default_server;
    		return 403;
 	 }   
}

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,293219,293219#msg-293219



More information about the nginx mailing list