주 메뉴 열기

wwiki β

바뀜

Nginx

7,428 바이트 추가됨, 2023년 11월 4일 (토) 01:01
에러
== 에러 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 =====<syntaxhighlight lang="nginx">location /some/path/ { proxy_pass http://www.example.com/link/;}</syntaxhighlight> ===== Passing Request Headers =====<syntaxhighlight lang="nginx">location /some/path/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://localhost:8000;}</syntaxhighlight>proxy_set_header 지시어를 사용하여 프록시된 서버로 전달되는 헤더 필드를 변경하거나 제거할 수 있습니다. 이 지시어는 location, server, http 블록 등에서 지정할 수 있습니다. 위 예제에서 [[HTTP#Host|Host]] 필드는 $proxy_host변수로 설정되었다. <syntaxhighlight lang="nginx">location /some/path/ { proxy_set_header Accept-Encoding ""; proxy_pass http://localhost:8000;} </syntaxhighlight>위 예제에서 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 ===<syntaxhighlight lang="nginx">Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }location @name { ... }Default: —Context: server, location</syntaxhighlight> ==== 예제 ====<syntaxhighlight lang="nginx">location = / { [ configuration A ]} location / { [ configuration B ]} location /documents/ { [ configuration C ]} location ^~ /images/ { [ configuration D ]} location ~* \.(gif|jpg|jpeg)$ { [ configuration E ]}</syntaxhighlight>“/” 를 요청하면 설정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 [[HTTP#X-Forwarded-For .28XFF.29|X-Forwarded-For]], [[HTTP#X-Forwarded-Proto .28XFP.29|X-Forwarded-Proto]], [[HTTP#X-Forwarded-Host .28XFH.29|X-Forwarded-Host]] 을 주로 설정하는데, HttpContext.Connection.RemoteIpAddress는 X-Forwarded-For 헤더 값을 사용하여 설정됩니다. HttpContext.Request.Scheme은 X-Forwarded-Proto 헤더 값을 사용하여 설정됩니다. HttpContext.Request.Host는 X-Forwarded-Host 헤더 값을 사용하여 설정됩니다.<syntaxhighlight lang="nginx">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;</syntaxhighlight> === 변수 === ==== proxy_add_x_forwarded_for ====<syntaxhighlight lang="nginx">proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;</syntaxhighlight>만약 클라이언트에서 request header에 [[HTTP#X-Forwarded-For .28XFF.29|X-Forwarded-For]]가 없다면 $proxy_add_x_forwarded_for는 $remote_addr 와 동일한데 실제 서버에서 보자면 요청하는 것은 프록시 서버이기 때문에 $remote_addr은 프록시 서버의 아이피가 되므로 실제 클라이언트 아이피를 서버가 알 수 있도록 하기 위해서 위의 설정이 필요하다. == [[CORS]] ==<syntaxhighlight lang="nginx">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;}</syntaxhighlight>[[HTTP#OPTIONS|OPTIONS]] 메서드에 대한 응답을 추가할 수 있다. == 보안설정 ==[[HTTP#보안|HTTP보안]] <nowiki>#</nowiki>/etc/nginx/nginx.conf === HSTS ===<syntaxhighlight lang="nginx">add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";</syntaxhighlight>=== 서버 버전 숨기기 등 ===<syntaxhighlight lang="nginx">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;}  </syntaxhighlight> === 첫 페이지 제거 ===<syntaxhighlight lang="nginx">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; } </syntaxhighlight> === 특정url제거 ===<syntaxhighlight lang="nginx">location /share2 {  return 404; } </syntaxhighlight> === 에러페이지 제거 ===<syntaxhighlight lang="nginx">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;} </syntaxhighlight> ==== error.html ====<syntaxhighlight lang="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> </syntaxhighlight> === 해외아이피 차단 ===nginx geo ip 모듈 설치 확인: --with-http_geoip_module=dynamic $ /usr/sbin/nginx -Vgeoip 데이터베이스 패키지 설치 $ sudo apt install geoip-database설정파일에 국가 추가하기: /etc/nginx/nginx.conf<syntaxhighlight lang="nginx"> #특정국가 IP 차단하기 geoip_country /usr/share/GeoIP/GeoIP.dat; map $geoip_country_code $allowed_country { default yes; RU no; CN no; }</syntaxhighlight>server블록에 추가<syntaxhighlight lang="nginx">if ($allowed_country = no) { return 444;}</syntaxhighlight>테스트: 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
[[분류:Web server]]
편집
2,431