Nginx

wwiki
이동: 둘러보기, 검색

Admin Guide[편집 | 원본 편집]

Web Server[편집 | 원본 편집]

NGINX Reverse Proxy (리버스 프록시)[편집 | 원본 편집]

https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

Passing a Request to a Proxied Server[편집 | 원본 편집]
location /some/path/ {
    proxy_pass http://www.example.com/link/;
}
Passing Request Headers[편집 | 원본 편집]
location /some/path/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://localhost:8000;
}

proxy_set_header 지시어를 사용하여 프록시된 서버로 전달되는 헤더 필드를 변경하거나 제거할 수 있습니다. 이 지시어는 location, server, http 블록 등에서 지정할 수 있습니다. 위 예제에서 Host 필드는 $proxy_host변수로 설정되었다.

location /some/path/ {
    proxy_set_header Accept-Encoding "";
    proxy_pass http://localhost:8000;
}

위 예제에서 Accept-Encoding 필드가 프록시된 서버로 전달되는 것을 방지하기 위하여 빈 문자열로 설정했다.

Nginx Proxy Manager를 사용할 수 있다.

Security Controls[편집 | 원본 편집]

Restricting Access with HTTP Basic Authentication[편집 | 원본 편집]

https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/

Creating a Password File[편집 | 원본 편집]

Apache와 Basic 인증으로 접근 제한하기

htpasswd 명령으로 만들 수 있다.

http core module[편집 | 원본 편집]

https://nginx.org/en/docs/http/ngx_http_core_module.html

location[편집 | 원본 편집]

Syntax:	location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:	
Context:	server, location

예제[편집 | 원본 편집]

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

“/” 를 요청하면 설정A가 매칭될 것이다.

“/index.html” 을 요청하면 설정B가 매칭될 것이다.

“/documents/document.html”을 요청하면 설정C가 매칭될 것이다.

“/images/1.gif”를 요청하면 설정D가 매칭될 것이다.

“/documents/1.jpg”를 요청하면 설정E가 매칭될 것이다.

변수[편집 | 원본 편집]

$scheme[편집 | 원본 편집]

request scheme, “http” or “https”

$host[편집 | 원본 편집]

클라이언트 요청 헤더의 Host 필드의 값

Nginx Http Proxy module[편집 | 원본 편집]

proxy_set_header[편집 | 원본 편집]

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header

X-Forwarded-For, X-Forwarded-Proto, X-Forwarded-Host 을 주로 설정하는데, HttpContext.Connection.RemoteIpAddress는 X-Forwarded-For 헤더 값을 사용하여 설정됩니다. HttpContext.Request.Scheme은 X-Forwarded-Proto 헤더 값을 사용하여 설정됩니다. HttpContext.Request.Host는 X-Forwarded-Host 헤더 값을 사용하여 설정됩니다.

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;

변수[편집 | 원본 편집]

proxy_add_x_forwarded_for[편집 | 원본 편집]

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

만약 클라이언트에서 request header에 X-Forwarded-For가 없다면 $proxy_add_x_forwarded_for는 $remote_addr 와 동일한데 실제 서버에서 보자면 요청하는 것은 프록시 서버이기 때문에 $remote_addr은 프록시 서버의 아이피가 되므로 실제 클라이언트 아이피를 서버가 알 수 있도록 하기 위해서 위의 설정이 필요하다.

CORS[편집 | 원본 편집]

location /api {
        # for OPTIONS return these headers and HTTP 200 status
        if ($request_method = OPTIONS ) {
            add_header Allow "OPTIONS";
            add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PATCH, OPTIONS';
            add_header Access-Control-Allow-Headers 'Content-Type, Authorization';
            add_header Access-Control-Allow-Origin "https://[your allow hostname]";
            return 204;
        }
        proxy_pass http://[your-hostname]/api;
}

OPTIONS 메서드에 대한 응답을 추가할 수 있다.

보안설정[편집 | 원본 편집]

HTTP보안

#/etc/nginx/nginx.conf

HSTS[편집 | 원본 편집]

add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

서버 버전 숨기기 등[편집 | 원본 편집]

http{
    # 서버 버전 숨기기
    server_tokens off; 
    # 개발언어 숨기기
    fastcgi_hide_header X-Powered-By;
    proxy_hide_header X-Powered-By;
    # http 프로토콜에서 xml 데이터를 전송하기위해 사용되는데, 특별한 용도가 없다면 막아두는것이 좋다.
    fastcgi_hide_header X-Pingback;
    proxy_hide_header X-Pingback;
    # xml 관련된 W3c 표준이긴한데, 특별한 용도가 없다면 막아두는것이 좋다.
    fastcgi_hide_header Link;
    proxy_hide_header X-Link;
}

첫 페이지 제거[편집 | 원본 편집]

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                return 404;
        }

특정url제거[편집 | 원본 편집]

location /share2 {

                return 404;
        }

에러페이지 제거[편집 | 원본 편집]

error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 495 496 497 500 501 502 503 504 505 506 507 /error.html;

location = /error.html {

                internal;
}

error.html[편집 | 원본 편집]

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >

</head>

<body>

<center>

<br><h1>ERROR</h1>

</center>

</body>

</html>

해외아이피 차단[편집 | 원본 편집]

nginx geo ip 모듈 설치 확인: --with-http_geoip_module=dynamic

$ /usr/sbin/nginx -V

geoip 데이터베이스 패키지 설치

$ sudo apt install geoip-database

설정파일에 국가 추가하기: /etc/nginx/nginx.conf

    #특정국가 IP 차단하기 
    geoip_country /usr/share/GeoIP/GeoIP.dat;
    map $geoip_country_code $allowed_country {
        default yes;
        RU no;
        CN no;
    }

server블록에 추가

if ($allowed_country = no) {
    return 444;
}

테스트: https://www.locabrowser.com/

에러[편집 | 원본 편집]

client intended to send too large body[편집 | 원본 편집]

server블록에 다음 추가

client_max_body_size 512M;

SSL routines:tls_process_client_hello:version too low[편집 | 원본 편집]

IE 6.0등 브라우저 버전이 낮은 경우에 발생한 보안에러이다.

외부링크[편집 | 원본 편집]

변수 인덱스: http://nginx.org/en/docs/varindex.html