주 메뉴 열기

wwiki β

바뀜

Nginx

4,934 바이트 추가됨, 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">
return 444;
}
</syntaxhighlight><br 테스트: https://www.locabrowser.com/>
==에러==
===SSL routines:tls_process_client_hello:version too low===
IE 6.0등 브라우저 버전이 낮은 경우에 발생한 보안에러이다.
 
== 외부링크 ==
변수 인덱스: http://nginx.org/en/docs/varindex.html
[[분류:Web server]]
편집
2,431