我正在使用Nginx建立一个既暴露API又
的网站。我想在网站上添加SSL证书,并且:
所有API流量都是唯一的,因为它在网站名称后调用 api 的网址:
我希望这可以通过Nginx服务器/位置块的组合来完成。如下所示:
# http
server {
listen 80;
server_name example.com www.example.com;
# direct all api traffic (www.example.com/api/) via http
location /api/ {
root /full_path_to_api_code # '/api/' location value will be appended
}
# redirect all non api traffic to https block
location / {
return 301 https://$server_name$request_uri;
}
}
# https
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
...
}
我希望' location / api /' block捕获所有api调用,并让它们保持原样不变。也许有更好的方法来做到这一点?
如果有人能告诉我这是否可行以及这是否是正确的方法,我将不胜感激?
答案 0 :(得分:0)
你的配置很好。你唯一可以改变的是将它们组合成相同的块,如果你可以在http或https上调用api。然后你可以像下面这样做
# http
server {
listen 80;
listen 443 ssl;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
server_name example.com www.example.com;
# direct all api traffic (www.example.com/api/) via http
location /api/ {
root /full_path_to_api_code
}
# redirect all non api traffic to https block
location / {
if ($scheme ="http") {
# redirect all non api traffic to https block
return 301 https://$server_name$request_uri;
}
normal https request processing
}
}
我仍然会使用您的配置,原因是if is evil