0%

nginx | https 绑定端口提供 restful api 服务

首先把域名解析到机器上,这里假设是 xxx.com

然后开启,restful api 服务,这里绑定的端口是 8081

nginx 可以这样配置。

1
2
3
4
5
6
7
8
9
10
11
server {
#listen 443;
server_name 域名;
listen 443 ssl;
#listen [::]:443 ssl default_server;
ssl_certificate /home/ubuntu/https/fullchain.crt;
ssl_certificate_key /home/ubuntu/https/private.pem;
location / {
proxy_pass http://127.0.0.1:8081/;
}
}

如果你想获取真实 IP 可以查看

如果要跨服务器可以这样配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
server {
listen 80;
server_name membership.thlm.bond;
listen 443 ssl;
ssl_certificate /home/ubuntu/https/fullchain.crt;
ssl_certificate_key /home/ubuntu/https/private.pem;
location / {

# 允许所有来源的跨域请求
if ($http_origin ~* (https?://(.+\.)?(thlm.com|vip.thlm.com)$)) {
add_header 'Access-Control-Allow-Origin' $http_origin;
}

add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Credentials' 'true';
proxy_pass http://103.119.106.13:15120/;
}
}
请我喝杯咖啡吧~