Last active
February 28, 2023 09:29
-
-
Save uupaa/100554c1c879e114610b to your computer and use it in GitHub Desktop.
nginx: Avoid CORS and reverse proxy settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http { | |
server { | |
listen 8080; | |
server_name localhost; | |
charset UTF-8; | |
# Avoid CORS and reverse proxy settings | |
# [1] xhr request: http://localhost:8080/api/xxx/yyy/zzz | |
# [2] /api/xxx/yyy/zzz | |
# [3] http://your-dev-server.example.com/api/xxx/yyy/zzz | |
location /api/ { # [2] | |
proxy_http_version 1.1; | |
proxy_pass http://your-dev-server.example.com/api/; # [3] | |
add_header Access-Control-Allow-Origin *; | |
add_header Access-Control-Allow-Methods "POST, GET, OPTIONS"; | |
add_header Access-Control-Allow-Headers "Origin, Authorization, Accept"; | |
add_header Access-Control-Allow-Credentials true; | |
# 以下のヘッダの差し替えたり差し込んだりすると | |
# reject される設定になっているケース(アクセス元のアドレスをしっかり見て制限しているとか)もあるので | |
# ダメなら外すといいよ | |
# proxy_set_header X-Real-IP $remote_addr; | |
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
# proxy_set_header Host $http_host; | |
# proxy_set_header X-NginX-Proxy true; | |
} | |
} | |
} | |
# サーバから404 や 500 が返ってきて、何が正解なのか分からなくなったら | |
# Wireshark でパケットをキャプチャし、 | |
# 200 とそれ以外のケースでリクエストヘッダの内容を比較すると、 | |
# あっさり解決できたりしますよ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment