기존의 모든 정적 파일을 Nginx와 직접 제공하는 방법이지만 나머지는 백엔드 서버로 프록시합니다.

StackOverflow https://stackoverflow.com/questions/869001

문제

location / {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    if (-f $request_filename) {
        access_log off;
        expires 30d;
        break;
        }

    if (!-f $request_filename) {
        proxy_pass http://127.0.0.1:8080; # backend server listening
        break;
        }
    }

위는 Nginx를 사용하여 직접 기존 파일을 직접 제공합니다 (예 : Nginx는 PHP 소스 코드 만 표시). 그렇지 않으면 Apache에 대한 요청을 전달합니다. *.php에 대한 요청도 Apache로 전달되어 처리되도록 규칙에서 *.php 파일을 배제해야합니다.

Nginx가 모든 정적 파일과 Apache를 처리하여 모든 동적 인 작업을 처리하기를 원합니다.

편집 : 흰색 목록 접근 방식이 있지만 매우 우아하지는 않습니다. 모든 확장 기능을보십시오. 나는 이것을 원하지 않습니다.

location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
    access_log off;
    expires 30d;
    }
location / {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
    }

편집 2 : 최신 버전의 Nginx 사용 try_files 대신에 http://wiki.nginx.org/httpcoremodule#try_files

도움이 되었습니까?

해결책

사용 try_files 그리고 이름이 지정된 위치 블록 ( '@apachesite'). 이렇게하면 불필요한 Regex 일치 및 IF 블록이 제거됩니다. 더 효율적입니다.

location / {
    root /path/to/root/of/static/files;
    try_files $uri $uri/ @apachesite;

    expires max;
    access_log off;
}

location @apachesite {
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

업데이트: 이 구성의 가정은 아래에 PHP 스크립트가 존재하지 않는다는 것입니다. /path/to/root/of/static/files. 이것은 대부분의 최신 PHP 프레임 워크에서 일반적입니다. 레거시 PHP 프로젝트에 동일한 폴더에 PHP 스크립트와 정적 파일이 혼합 된 경우 Nginx가 제공하려는 모든 파일 유형을 화이트리스트로 만들어야 할 수도 있습니다.

다른 팁

이 시도:

location / {
    root /path/to/root;
    expires 30d;
    access_log off;
}

location ~* ^.*\.php$ {
    if (!-f $request_filename) {
        return 404;
    }
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

잘만되면 그것은 작동합니다. 일반 표현식은 일반 문자열보다 우선 순위가 높기 때문에 모든 요청이 끝납니다. .php 해당하는 경우 Apache로 구조해야합니다 .php 파일이 존재. REST는 정적 파일로 처리됩니다. 위치 평가의 실제 알고리즘은 다음과 같습니다 여기.

mod_rewrite를 사용하여 스크립트의 확장을 숨기거나 /로 끝나는 예쁜 URL을 좋아하는 경우 다른 방향에서이를 접근 할 수 있습니다. Nginx에게 비 정적 확장 기능이있는 모든 것이 아파치로 넘어가도록 지시하십시오. 예를 들어:

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$
{
    root   /path/to/static-content;
}

location ~* ^!.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
    if (!-f $request_filename) {
        return 404;
    }
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://127.0.0.1:8080;
}

이 스 니펫의 첫 부분을 다음에서 찾았습니다. http://code.google.com/p/scalr/wiki/nginxstatic

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top