programing

NGINX를 사용하여 기존의 모든 정적 파일을 직접 제공하지만 나머지는 백엔드 서버에 프록시하는 방법.

nasanasas 2020. 10. 16. 07:29
반응형

NGINX를 사용하여 기존의 모든 정적 파일을 직접 제공하지만 나머지는 백엔드 서버에 프록시하는 방법.


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'). 이렇게하면 불필요한 정규식 일치 및 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;
}

업데이트 : 이 구성의 가정은 /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해당 .php파일 존재 하는 경우 아파치로 끝나는 모든 요청을 인식 해야 합니다. 나머지는 정적 파일로 처리됩니다. 위치를 평가하는 실제 알고리즘은 여기에 있습니다 .


If you use mod_rewrite to hide the extension of your scripts, or if you just like pretty URLs that end in /, then you might want to approach this from the other direction. Tell nginx to let anything with a non-static extension to go through to apache. For example:

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;
}

I found the first part of this snippet over at: http://code.google.com/p/scalr/wiki/NginxStatic

참고URL : https://stackoverflow.com/questions/869001/how-to-serve-all-existing-static-files-directly-with-nginx-but-proxy-the-rest-t

반응형