nginx:在server中配置多个location,避免跨域访问


upstream test {
     server 127.0.0.1:9080 weight=1;     
     #server 192.168.4.68:80 weight=1;
     #ip_hash;                           
 }             


    server {
         listen       80;
         server_name  jsjnks.test.com;
         charset utf-8;
         location / {
             proxy_pass http://test;
             proxy_set_header Host      $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_redirect  off; 
         }        
         location /public {
             alias  "D:/app/nginx/html/public";
         }    
         location /jsjnks-static {
             alias  "D:/app/nginx/html/test-static";
         }            
     }

public,test-static都是静态资源目录,域名时static.test.com.

用上面配置后,就避免了JS等静态资源的跨域访问,都处于jsjnks.test.com这一个域名路径之下。

注意红色部分一定要正确!

原文地址:https://www.cnblogs.com/huiy/p/13334958.html