直接上設定
server { listen 80; listen [::]:80; server_name api.example.com, example.com; location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; set $proxy http://0.0.0.0:3000; if ($host ~ api.example.com) { set $proxy http://0.0.0.0:8000; } proxy_pass $proxy; } }
...
Category: Nginx
處理 CORS 算是許多 Web 開發者會處理的情況
其中要又有一個比較重要的部分就是要設定 Access-Control-Allow-Origin 這個允許來源的 header
通常為了安全性問題, 都是只會設定允許的 host 上去
但是常常設定而且在不同環境設定也是有點麻煩
所以這邊就用了直接在 server 設定的方式來處理
這樣直接看 server 的 rule 設定即可
也可以快速的條列不同環境的 host
# 用法 if ($http_referer ~* example.com) { add_header 'Access-Control-Allow-Origin' 'https://example.com'; } if ($http_referer ~* dev.example.com) { add_header 'Access-Control-Allow-Origin' 'https://dev.example.com'; } 這個判斷沒有固定的寫法
可以參考底下 Nginx 對 http_referer 的介紹來針對需要判斷 host 的方式進行修改
Refer - 跨來源資源共用(CORS)
Refer - Module ngx_http_referer_module
...
# Let’s Encrypt - Has a certificate problem in Android Chrome If you use Let’s Encrypt as SSL certificate
Then setting this in Nginx
ssl_certificate /etc/letsencrypt/live/<domain>/cert.pem; You can find it maybe has problem in Android Chrome
Now you can modify this
ssl_certificate /etc/letsencrypt/live/<domain>/fullchain.pem; ## Test your SSL SSL Server Test
Refer - Let’s encrypt certificate not working for Andriod’s Google Chrome
...
# nginx - Study Log 半調子的紀錄…
## Command Line sudo service nginx start sudo service nginx restart sudo service nginx stop # 測試 nginx 設定 sudo nginx -t ## config # document root root /usr/share/nginx/html; # set page show index index.html index.htm index.php; # 把 lib 目錄打開 location /lib { autoindex on; } refer - Can I hide all server / os info?
...