Springmvc nginx 동정 분리 과정 상세 설명 실현

아래 에서 스스로 정리 하고 잘못 이 있 으 면 지적 하여 바로잡아 주 십시오
일반적인 nginx 의 정적 파일 항목 은 이렇게 설정 되 어 있 습 니 다.

location ~ .*\.(js|css)?$
{
  root E:/Workspaces/Idea15/demo/web/WEB-INF;
  expires 1h;
}
그러나 이렇게 설정 하면 시스템 에서 해당 하 는 파일 을 읽 을 수 없습니다.springmvc 자체 의 전단 템 플 릿 이 정적 자원 Handles 에 접근 하도록 설정 되 어 있 기 때 문 입 니 다.
어떻게 nginx 를 이용 하여 접근 하여 동정 분 리 를 실현 합 니까?
nginx 에이전트 구축
첫 번 째 단계,nginx.conf 파일 수정

#location / {
  #root  html;
  #index index.html index.htm;
#}
location / {
  proxy_next_upstream http_502 http_504 error timeout invalid_header;
  proxy_pass  http://127.0.0.1:8080;
  #       IP
  proxy_set_header  X-Real-IP    $remote_addr;
  #     Host  
  proxy_set_header  Host       $host;
  #       ,   IP     
  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
  #          
  proxy_set_header  X-Forwarded-Proto $scheme;
}
이렇게 교체 하면 nginx 대 리 를 통 해 tomcat 로 이동 할 수 있 지만 정적 자원 은 tomcat 를 통 해 읽 을 수 있 습 니 다.
메모:원본 프로젝트 파일 을 변경 할 필요 가 없습니다.tomcat 설정 을 수정 해 야 합 니 다.bin/server.xml 은 포트 번 호 를 80 에서 8080 으로 바 꿉 니 다.
정적 파일 처리
두 번 째 단계,동정 분리 실현
springmvc 의 전단 컨트롤 러 에 다음 과 같이 설정 합 니 다.



nginx 에서 다음 과 같이 설정 합 니 다.demo 는 제 프로젝트 이름 입 니 다.다음 세 가지 방식 으로 구분 할 수 있 습 니 다.

location demo/image/ {
  root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location /css/ {
  root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
location js/ {
  root E:/Workspaces/Idea15/demo/web/WEB-INF;
}
근 데'demo/image'라 고 쓰 면 안 돼 요.
주:404 페이지 등 springmvc 프로젝트 에 설정 되 어 있 으 면 여기 서 계속 설정 할 필요 가 없습니다.

최종 nginx.conf 페이지 코드 는 다음 과 같 습 니 다.

#user nobody;
worker_processes 1;

events {
  worker_connections 1024;
}

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

  #access_log logs/access.log main;

  sendfile    on;
  #tcp_nopush   on;

  #keepalive_timeout 0;
  keepalive_timeout 65;

  #gzip on;

  server {
    listen    80;
    server_name localhost;

    location / {
      proxy_next_upstream http_502 http_504 error timeout invalid_header;
      proxy_pass  http://127.0.0.1:8080;
      #       IP
      proxy_set_header  X-Real-IP    $remote_addr;
      #     Host  
      proxy_set_header  Host       $host;
      #       ,   IP     
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      #          
      proxy_set_header  X-Forwarded-Proto $scheme;
    }

    location image/ {
      root E:/Workspaces/Idea15/demo/web/WEB-INF;
    }
    location css/ {
      root E:/Workspaces/Idea15/demo/web/WEB-INF;
    }
    location js/ {
      root E:/Workspaces/Idea15/demo/web/WEB-INF;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504 /50x.html;
    location = /50x.html {
      root  html;
    }
  }

}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기