{"id":329493,"date":"2022-02-13T15:00:14","date_gmt":"2022-02-13T15:00:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=329493"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=329493","title":{"rendered":"<span>cors-anywhere \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0435 nginx<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 CORS, \u0442\u043e \u0437\u043d\u0430\u0435\u0442\u0435 \u0432\u0441\u044e \u0442\u0443 \u0431\u043e\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u0441\u043f\u044b\u0442\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043a API \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c \u0434\u043e\u043c\u0435\u043d\u0435. \u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043a\u0430\u043a\u043e\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f <a href=\"https:\/\/github.com\/Rob--W\/cors-anywhere\" rel=\"noopener noreferrer nofollow\">cors-anywhere<\/a>.<\/p>\n<blockquote>\n<p>\u041f\u044f\u0442\u043d\u0438\u0446\u0430 \u0432\u0435\u0447\u0435\u0440 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0447\u0435\u0433\u043e<\/p>\n<\/blockquote>\n<p>\u041d\u0435 \u043c\u043d\u043e\u0433\u0438\u043c \u0438\u0437\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0430 <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_pass\" rel=\"noopener noreferrer nofollow\">proxy_pass<\/a> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u043c\u0435\u043d\u044b \u0438 \u043f\u043e\u0442\u043e\u043a\u0438 (aka <code>upstream<\/code>), \u043d\u043e \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"nginx\">proxy_pass https:\/\/api.github.com\/$request_uri<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0437\u0430\u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0438\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 (\u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0430\u043c\u0438) \u043a\u043e\u043d\u0444\u0438\u0433 \u0434\u043b\u044f nginx, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043b\u044e\u0431\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u043c\u0435\u043d.<\/p>\n<h2>\u0427\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c<\/h2>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043e\u0439 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_map_module.html#map\" rel=\"noopener noreferrer nofollow\">map<\/a>:<\/p>\n<pre><code class=\"nginx\">map $request_url $my_request_path {   ~*\/(.*)$ $1;   default  \"\"; }<\/code><\/pre>\n<p>\u0422\u0430\u043a, \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a <code>http:\/\/example.com\/api<\/code> \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>$my_request_path<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c <code>api<\/code>.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_headers_module.html#add_header\" rel=\"noopener noreferrer nofollow\">add_header<\/a>:<\/p>\n<pre><code class=\"nginx\">add_header X-Request-Path $my_request_path always;<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>X-Request-Path<\/code> \u0441 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>api<\/code>.<\/p>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u044b <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_set_header\" rel=\"noopener noreferrer nofollow\">proxy_set_header<\/a> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>proxy_pass<\/code>. \u0410 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_hide_header\" rel=\"noopener noreferrer nofollow\">proxy_hide_header<\/a><strong> <\/strong>\u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c<strong> <\/strong>\u0437\u0430\u0433\u043e\u043b\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442 <code>proxy_pass<\/code>.<\/p>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u044b <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_rewrite_module.html#if\" rel=\"noopener noreferrer nofollow\">if<\/a> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>OPTIONS<\/code> \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430:<\/p>\n<pre><code class=\"nginx\">if ($request_method = OPTIONS) {   return 204; }<\/code><\/pre>\n<h2>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u044a\u044f\u0432\u0438\u043c <code>$proxy_uri<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0437 <code>$request_uri<\/code>:<\/p>\n<pre><code class=\"nginx\">map $request_uri $proxy_uri {   ~*\/http:\/\/(.*)\/(.+)$  \"http:\/\/$1\/$2\";   ~*\/https:\/\/(.*)\/(.+)$ \"https:\/\/$1\/$2\";   ~*\/http:\/\/(.*)$       \"http:\/\/$1\/\";   ~*\/https:\/\/(.*)$      \"https:\/\/$1\/\";   ~*\/(.*)\/(.+)$         \"https:\/\/$1\/$2\";   ~*\/(.*)$              \"https:\/\/$1\/\";   default               \"\"; }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a: \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 <code>http:\/\/example.com\/example.ru<\/code>, \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>$proxy_uri<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c <code>https:\/\/example.ru<\/code><\/p>\n<p>\u0418\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e <code>$proxy_uri<\/code> \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043c \u0447\u0430\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0443 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Origin\" rel=\"noopener noreferrer nofollow\">Origin<\/a>:<\/p>\n<pre><code class=\"nginx\">map $proxy_uri $proxy_origin {   ~*(.*)\/.*$ $1;   default    \"\"; }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Forwarded\" rel=\"noopener noreferrer nofollow\">Forwarded<\/a> \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 2 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"nginx\">map $remote_addr $proxy_forwarded_addr {   ~^[0-9.]+$        \"for=$remote_addr\";   ~^[0-9A-Fa-f:.]+$ \"for=\\\"[$remote_addr]\\\"\";   default           \"for=unknown\"; }  map $http_forwarded $proxy_add_forwarded {   \"\"      \"$proxy_forwarded_addr\";   default \"$http_forwarded, $proxy_forwarded_addr\"; }<\/code><\/pre>\n<blockquote>\n<p>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\u0430 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/X-Forwarded-For\" rel=\"noopener noreferrer nofollow\">X-Forwarded-For<\/a> \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0432 <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#variables\" rel=\"noopener noreferrer nofollow\">nginx<\/a><\/p>\n<\/blockquote>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044e \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"nginx\">server {   listen 443 ssl;       server_name cors.example.com;      proxy_http_version         1.1;   proxy_pass_request_headers on;   proxy_pass_request_body    on;      proxy_redirect             off;   resolver                   77.88.8.8 77.88.8.1 8.8.8.8 8.8.4.4 valid=1d;      location \/ {     if ($proxy_uri = \"\") {       # empty uri       return 403;     }          # add proxy cors headers     add_header Access-Control-Allow-Headers \"*\" always;     add_header Access-Control-Allow-Methods \"*\" always;     add_header Access-Control-Allow-Origin  \"*\" always;      if ($request_method = OPTIONS) {       return 204;     }          proxy_set_header Host                $proxy_host;     proxy_set_header Origin              $proxy_origin;     proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;     proxy_set_header X-Forwarded-Proto   $scheme;     proxy_set_header Forwarded           \"$proxy_add_forwarded;proto=$scheme\";          proxy_pass $proxy_uri;   } }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Preflight_request\" rel=\"noopener noreferrer nofollow\">CORS Preflight Request<\/a> \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438.<\/p>\n<h2>\u0414\u0435\u043b\u0430\u0435\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u043e<\/h2>\n<p>\u0412\u0441\u0435 \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u044b \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u0435\u043c, \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 CORS, \u0442\u043e \u0435\u0433\u043e \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043a\u0440\u043e\u0435\u043c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435:<\/p>\n<pre><code class=\"nginx\"># hide original cors proxy_hide_header Access-Control-Allow-Credentials; proxy_hide_header Access-Control-Allow-Headers; proxy_hide_header Access-Control-Allow-Methods; proxy_hide_header Access-Control-Allow-Origin; proxy_hide_header Access-Control-Expose-Headers; proxy_hide_header Access-Control-Max-Age; proxy_hide_header Access-Control-Request-Headers; proxy_hide_header Access-Control-Request-Method;<\/code><\/pre>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u0435\u0449\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c IP \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u0445\u043e\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c rate limit, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443:<\/p>\n<pre><code class=\"nginx\">proxy_set_header X-Real-IP           $remote_addr; proxy_set_header X-Client-IP         $remote_addr; proxy_set_header CF-Connecting-IP    $remote_addr; proxy_set_header Fastly-Client-IP    $remote_addr; proxy_set_header True-Client-IP      $remote_addr; proxy_set_header X-Cluster-Client-IP $remote_addr;<\/code><\/pre>\n<blockquote>\n<p>\u041c\u044b \u0436\u0435 \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u043e\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u043e?)<\/p>\n<\/blockquote>\n<p>\u0418, \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u0432 \u043a\u044d\u0448\/\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e\/etc:<\/p>\n<pre><code class=\"nginx\">sendfile                   on; tcp_nodelay                on; tcp_nopush                 on;  etag                       off; if_modified_since          off;  proxy_buffering            off; proxy_cache                off; proxy_cache_convert_head   off; proxy_max_temp_file_size   0; client_max_body_size       0;  proxy_read_timeout         1m; proxy_connect_timeout      1m; reset_timedout_connection  on;  gzip                       off; gzip_proxied               off; # brotli                   off;<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u043d\u0444\u0438\u0433 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"nginx\">map $request_uri $proxy_uri {   ~*\/http:\/\/(.*)\/(.+)$  \"http:\/\/$1\/$2\";   ~*\/https:\/\/(.*)\/(.+)$ \"https:\/\/$1\/$2\";   ~*\/http:\/\/(.*)$       \"http:\/\/$1\/\";   ~*\/https:\/\/(.*)$      \"https:\/\/$1\/\";   ~*\/(.*)\/(.+)$         \"https:\/\/$1\/$2\";   ~*\/(.*)$              \"https:\/\/$1\/\";   default               \"\"; }  map $proxy_uri $proxy_origin {   ~*(.*)\/.*$ $1;   default    \"\"; }  map $remote_addr $proxy_forwarded_addr {   ~^[0-9.]+$        \"for=$remote_addr\";   ~^[0-9A-Fa-f:.]+$ \"for=\\\"[$remote_addr]\\\"\";   default           \"for=unknown\"; }  map $http_forwarded $proxy_add_forwarded {   \"\"      \"$proxy_forwarded_addr\";   default \"$http_forwarded, $proxy_forwarded_addr\"; }  server {   listen 443 ssl;      ssl_certificate \/etc\/letsencrypt\/live\/cors.example.com\/fullchain.pem;   ssl_certificate_key \/etc\/letsencrypt\/live\/cors.example.com\/privkey.pem;   ssl_trusted_certificate \/etc\/letsencrypt\/live\/cors.example.com\/chain.pem;      server_name cors.example.com;      sendfile                   on;   tcp_nodelay                on;   tcp_nopush                 on;      etag                       off;   if_modified_since          off;      proxy_buffering            off;   proxy_cache                off;   proxy_cache_convert_head   off;   proxy_max_temp_file_size   0;   client_max_body_size       0;      proxy_http_version         1.1;   proxy_pass_request_headers on;   proxy_pass_request_body    on;      proxy_read_timeout         1m;   proxy_connect_timeout      1m;   reset_timedout_connection  on;      proxy_redirect             off;   resolver                   77.88.8.8 77.88.8.1 8.8.8.8 8.8.4.4 valid=1d;      gzip                       off;   gzip_proxied               off;   # brotli                   off;      location \/ {     if ($proxy_uri = \"\") {       return 403;     }          # add proxy cors     add_header Access-Control-Allow-Headers \"*\" always;     add_header Access-Control-Allow-Methods \"*\" always;     add_header Access-Control-Allow-Origin  \"*\" always;      if ($request_method = \"OPTIONS\") {       return 204;     }          # pass client to proxy     proxy_set_header Host                $proxy_host;     proxy_set_header Origin              $proxy_origin;     proxy_set_header X-Real-IP           $remote_addr;     proxy_set_header X-Client-IP         $remote_addr;     proxy_set_header CF-Connecting-IP    $remote_addr;     proxy_set_header Fastly-Client-IP    $remote_addr;     proxy_set_header True-Client-IP      $remote_addr;     proxy_set_header X-Cluster-Client-IP $remote_addr;     proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;     proxy_set_header X-Forwarded-Proto   $scheme;     proxy_set_header Forwarded           \"$proxy_add_forwarded;proto=$scheme\";          # hide original cors     proxy_hide_header Access-Control-Allow-Credentials;     proxy_hide_header Access-Control-Allow-Headers;     proxy_hide_header Access-Control-Allow-Methods;     proxy_hide_header Access-Control-Allow-Origin;     proxy_hide_header Access-Control-Expose-Headers;     proxy_hide_header Access-Control-Max-Age;     proxy_hide_header Access-Control-Request-Headers;     proxy_hide_header Access-Control-Request-Method;          proxy_pass $proxy_uri;   } }<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/651253\/\"> https:\/\/habr.com\/ru\/post\/651253\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0441 CORS, \u0442\u043e \u0437\u043d\u0430\u0435\u0442\u0435 \u0432\u0441\u044e \u0442\u0443 \u0431\u043e\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u0441\u043f\u044b\u0442\u044b\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043a API \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u043c \u0434\u043e\u043c\u0435\u043d\u0435. \u0415\u0441\u043b\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0434\u043b\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043a\u0430\u043a\u043e\u0435-\u043d\u0438\u0431\u0443\u0434\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f <a href=\"https:\/\/github.com\/Rob--W\/cors-anywhere\" rel=\"noopener noreferrer nofollow\">cors-anywhere<\/a>.<\/p>\n<blockquote>\n<p>\u041f\u044f\u0442\u043d\u0438\u0446\u0430 \u0432\u0435\u0447\u0435\u0440 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0447\u0435\u0433\u043e<\/p>\n<\/blockquote>\n<p>\u041d\u0435 \u043c\u043d\u043e\u0433\u0438\u043c \u0438\u0437\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u0430 <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_pass\" rel=\"noopener noreferrer nofollow\">proxy_pass<\/a> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u043e\u043c\u0435\u043d\u044b \u0438 \u043f\u043e\u0442\u043e\u043a\u0438 (aka <code>upstream<\/code>), \u043d\u043e \u0438 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"nginx\">proxy_pass https:\/\/api.github.com\/$request_uri<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0437\u0430\u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0438\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 (\u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043e\u0433\u043e\u0432\u043e\u0440\u043a\u0430\u043c\u0438) \u043a\u043e\u043d\u0444\u0438\u0433 \u0434\u043b\u044f nginx, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043b\u044e\u0431\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0439 \u0434\u043e\u043c\u0435\u043d.<\/p>\n<h2>\u0427\u0435\u043c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c<\/h2>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0445 c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043e\u0439 \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_map_module.html#map\" rel=\"noopener noreferrer nofollow\">map<\/a>:<\/p>\n<pre><code class=\"nginx\">map $request_url $my_request_path {   ~*\/(.*)$ $1;   default  \"\"; }<\/code><\/pre>\n<p>\u0422\u0430\u043a, \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043a <code>http:\/\/example.com\/api<\/code> \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>$my_request_path<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c <code>api<\/code>.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_headers_module.html#add_header\" rel=\"noopener noreferrer nofollow\">add_header<\/a>:<\/p>\n<pre><code class=\"nginx\">add_header X-Request-Path $my_request_path always;<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0434\u043e\u0431\u0430\u0432\u0438\u043b\u0441\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <code>X-Request-Path<\/code> \u0441 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>api<\/code>.<\/p>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u044b <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_set_header\" rel=\"noopener noreferrer nofollow\">proxy_set_header<\/a> \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f <code>proxy_pass<\/code>. \u0410 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#proxy_hide_header\" rel=\"noopener noreferrer nofollow\">proxy_hide_header<\/a><strong> <\/strong>\u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c<strong> <\/strong>\u0437\u0430\u0433\u043e\u043b\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043e\u0442 <code>proxy_pass<\/code>.<\/p>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u044b <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_rewrite_module.html#if\" rel=\"noopener noreferrer nofollow\">if<\/a> \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>OPTIONS<\/code> \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0443\u0436\u043d\u044b\u0439 \u043a\u043e\u0434 \u043e\u0442\u0432\u0435\u0442\u0430:<\/p>\n<pre><code class=\"nginx\">if ($request_method = OPTIONS) {   return 204; }<\/code><\/pre>\n<h2>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0432\u043c\u0435\u0441\u0442\u0435<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u044a\u044f\u0432\u0438\u043c <code>$proxy_uri<\/code> \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0438\u0437 <code>$request_uri<\/code>:<\/p>\n<pre><code class=\"nginx\">map $request_uri $proxy_uri {   ~*\/http:\/\/(.*)\/(.+)$  \"http:\/\/$1\/$2\";   ~*\/https:\/\/(.*)\/(.+)$ \"https:\/\/$1\/$2\";   ~*\/http:\/\/(.*)$       \"http:\/\/$1\/\";   ~*\/https:\/\/(.*)$      \"https:\/\/$1\/\";   ~*\/(.*)\/(.+)$         \"https:\/\/$1\/$2\";   ~*\/(.*)$              \"https:\/\/$1\/\";   default               \"\"; }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a: \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 <code>http:\/\/example.com\/example.ru<\/code>, \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>$proxy_uri<\/code> \u0431\u0443\u0434\u0435\u0442 \u043b\u0435\u0436\u0430\u0442\u044c <code>https:\/\/example.ru<\/code><\/p>\n<p>\u0418\u0437 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e <code>$proxy_uri<\/code> \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043c \u0447\u0430\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0443 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Origin\" rel=\"noopener noreferrer nofollow\">Origin<\/a>:<\/p>\n<pre><code class=\"nginx\">map $proxy_uri $proxy_origin {   ~*(.*)\/.*$ $1;   default    \"\"; }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0430 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/Forwarded\" rel=\"noopener noreferrer nofollow\">Forwarded<\/a> \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 2 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435:<\/p>\n<pre><code class=\"nginx\">map $remote_addr $proxy_forwarded_addr {   ~^[0-9.]+$        \"for=$remote_addr\";   ~^[0-9A-Fa-f:.]+$ \"for=\\\"[$remote_addr]\\\"\";   default           \"for=unknown\"; }  map $http_forwarded $proxy_add_forwarded {   \"\"      \"$proxy_forwarded_addr\";   default \"$http_forwarded, $proxy_forwarded_addr\"; }<\/code><\/pre>\n<blockquote>\n<p>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a\u0430 <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Headers\/X-Forwarded-For\" rel=\"noopener noreferrer nofollow\">X-Forwarded-For<\/a> \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u0432 <a href=\"https:\/\/nginx.org\/en\/docs\/http\/ngx_http_proxy_module.html#variables\" rel=\"noopener noreferrer nofollow\">nginx<\/a><\/p>\n<\/blockquote>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u044e \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430:<\/p>\n<pre><code class=\"nginx\">server {   listen 443 ssl;       server_name cors.example.com;      proxy_http_version         1.1;   proxy_pass_request_headers on;   proxy_pass_request_body    on;      proxy_redirect             off;   resolver                   77.88.8.8 77.88.8.1 8.8.8.8 8.8.4.4 valid=1d;      location \/ {     if ($proxy_uri = \"\") {       # empty uri       return 403;     }          # add proxy cors headers     add_header Access-Control-Allow-Headers \"*\" always;     add_header Access-Control-Allow-Methods \"*\" always;     add_header Access-Control-Allow-Origin  \"*\" always;      if ($request_method = OPTIONS) {       return 204;     }          proxy_set_header Host                $proxy_host;     proxy_set_header Origin              $proxy_origin;     proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;     proxy_set_header X-Forwarded-Proto   $scheme;     proxy_set_header Forwarded           \"$proxy_add_forwarded;proto=$scheme\";          proxy_pass $proxy_uri;   } }<\/code><\/pre>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Glossary\/Preflight_request\" rel=\"noopener noreferrer nofollow\">CORS Preflight Request<\/a> \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438.<\/p>\n<h2>\u0414\u0435\u043b\u0430\u0435\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u043e<\/h2>\n<p>\u0412\u0441\u0435 \u0431\u044b \u0445\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0435\u0441\u043b\u0438 \u0443 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u044b \u043f\u0440\u043e\u043a\u0441\u0438\u0440\u0443\u0435\u043c, \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043d\u0430 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 CORS, \u0442\u043e \u0435\u0433\u043e \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043a\u0440\u043e\u0435\u043c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435:<\/p>\n<pre><code class=\"nginx\"># hide original cors proxy_hide_header Access-Control-Allow-Credentials; proxy_hide_header Access-Control-Allow-Headers; proxy_hide_header Access-Control-Allow-Methods; proxy_hide_header Access-Control-Allow-Origin; proxy_hide_header Access-Control-Expose-Headers; proxy_hide_header Access-Control-Max-Age; proxy_hide_header Access-Control-Request-Headers; proxy_hide_header Access-Control-Request-Method;<\/code><\/pre>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e \u0431\u044b \u0435\u0449\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c IP \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u0445\u043e\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c rate limit, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0435\u0441\u0443\u0440\u0441\u0443:<\/p>\n<pre><code class=\"nginx\">proxy_set_header X-Real-IP           $remote_addr; proxy_set_header X-Client-IP         $remote_addr; proxy_set_header CF-Connecting-IP    $remote_addr; proxy_set_header Fastly-Client-IP    $remote_addr; proxy_set_header True-Client-IP      $remote_addr; proxy_set_header X-Cluster-Client-IP $remote_addr;<\/code><\/pre>\n<blockquote>\n<p>\u041c\u044b \u0436\u0435 \u043d\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043f\u0440\u043e \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u043e\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u043e?)<\/p>\n<\/blockquote>\n<p>\u0418, \u043d\u0430\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u043a, \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043a\u043b\u044e\u0447\u0438\u0432 \u043a\u044d\u0448\/\u0431\u0443\u0444\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u044e\/etc:<\/p>\n<pre><code class=\"nginx\">sendfile                   on; tcp_nodelay                on; tcp_nopush                 on;  etag                       off; if_modified_since          off;  proxy_buffering            off; proxy_cache                off; proxy_cache_convert_head   off; proxy_max_temp_file_size   0; client_max_body_size       0;  proxy_read_timeout         1m; proxy_connect_timeout      1m; reset_timedout_connection  on;  gzip                       off; gzip_proxied               off; # brotli                   off;<\/code><\/pre>\n<details class=\"spoiler\">\n<summary>\u041a\u043e\u043d\u0444\u0438\u0433 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"nginx\">map $request_uri $proxy_uri {   ~*\/http:\/\/(.*)\/(.+)$  \"http:\/\/$1\/$2\";   ~*\/https:\/\/(.*)\/(.+)$ \"https:\/\/$1\/$2\";   ~*\/http:\/\/(.*)$       \"http:\/\/$1\/\";   ~*\/https:\/\/(.*)$      \"https:\/\/$1\/\";   ~*\/(.*)\/(.+)$         \"https:\/\/$1\/$2\";   ~*\/(.*)$              \"https:\/\/$1\/\";   default               \"\"; }  map $proxy_uri $proxy_origin {   ~*(.*)\/.*$ $1;   default    \"\"; }  map $remote_addr $proxy_forwarded_addr {   ~^[0-9.]+$        \"for=$remote_addr\";   ~^[0-9A-Fa-f:.]+$ \"for=\\\"[$remote_addr]\\\"\";   default           \"for=unknown\"; }  map $http_forwarded $proxy_add_forwarded {   \"\"      \"$proxy_forwarded_addr\";   default \"$http_forwarded, $proxy_forwarded_addr\"; }  server {   listen 443 ssl;      ssl_certificate \/etc\/letsencrypt\/live\/cors.example.com\/fullchain.pem;   ssl_certificate_key \/etc\/letsencrypt\/live\/cors.example.com\/privkey.pem;   ssl_trusted_certificate \/etc\/letsencrypt\/live\/cors.example.com\/chain.pem;      server_name cors.example.com;      sendfile                   on;   tcp_nodelay                on;   tcp_nopush                 on;      etag                       off;   if_modified_since          off;      proxy_buffering            off;   proxy_cache                off;   proxy_cache_convert_head   off;   proxy_max_temp_file_size   0;   client_max_body_size       0;      proxy_http_version         1.1;   proxy_pass_request_headers on;   proxy_pass_request_body    on;      proxy_read_timeout         1m;   proxy_connect_timeout      1m;   reset_timedout_connection  on;      proxy_redirect             off;   resolver                   77.88.8.8 77.88.8.1 8.8.8.8 8.8.4.4 valid=1d;      gzip                       off;   gzip_proxied               off;   # brotli                   off;      location \/ {     if ($proxy_uri = \"\") {       return 403;     }          # add proxy cors     add_header Access-Control-Allow-Headers \"*\" always;     add_header Access-Control-Allow-Methods \"*\" always;     add_header Access-Control-Allow-Origin  \"*\" always;      if ($request_method = \"OPTIONS\") {       return 204;     }          # pass client to proxy     proxy_set_header Host                $proxy_host;     proxy_set_header Origin              $proxy_origin;     proxy_set_header X-Real-IP           $remote_addr;     proxy_set_header X-Client-IP         $remote_addr;     proxy_set_header CF-Connecting-IP    $remote_addr;     proxy_set_header Fastly-Client-IP    $remote_addr;     proxy_set_header True-Client-IP      $remote_addr;     proxy_set_header X-Cluster-Client-IP $remote_addr;     proxy_set_header X-Forwarded-For     $proxy_add_x_forwarded_for;     proxy_set_header X-Forwarded-Proto   $scheme;     proxy_set_header Forwarded           \"$proxy_add_forwarded;proto=$scheme\";          # hide original cors     proxy_hide_header Access-Control-Allow-Credentials;     proxy_hide_header Access-Control-Allow-Headers;     proxy_hide_header Access-Control-Allow-Methods;     proxy_hide_header Access-Control-Allow-Origin;     proxy_hide_header Access-Control-Expose-Headers;     proxy_hide_header Access-Control-Max-Age;     proxy_hide_header Access-Control-Request-Headers;     proxy_hide_header Access-Control-Request-Method;          proxy_pass $proxy_uri;   } }<\/code><\/pre>\n<\/div>\n<\/details>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/651253\/\"> https:\/\/habr.com\/ru\/post\/651253\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-329493","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329493"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329493\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}