CentOS7安装Nginx实现API网关

参考

http://nginx.org/

http://nginx.org/en/linux_packages.html#stable

https://www.npmjs.com/package/json-server

 

安装

yum install openssl zlib pcre

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install nginx

 

命令行启动

nginx –h

nginx

nginx -s stop

 

服务模式

systemctl start nginx

systemctl status nginx

systemctl enable nginx

systemctl stop nginx

 

浏览器查看

http://hostname

 

 

安装json-server模拟API服务

npm install json-server –g

echo "json-server a.json -H 192.168.1.6 -p 3000" > a-server.sh

echo "json-server b.json -H 192.168.1.6 -p 3001" > b-server.sh

chmod a+x a-server.sh b-server.sh

vi a.json

vi b.json

 

a.json 参考代码

{

"posts": [

{ "id": 1, "title": "json-server", "author": "typicode" }

]

}

 

b.json参考代码

{

"comments": [

{ "id": 1, "body": "some comment", "postId": 1 }

]

}

 

启动模拟api服务

./a-server.sh

./b-server.sh

 

配置Nginx为API网关

vim /etc/nginx/conf.d/a.conf

a.conf参考代码

server {

listen 80;

server_name 192.168.1.6;

 

location / {

proxy_pass http://192.168.1.6:3000/;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $host;

}

 

location /aaa/ {

proxy_pass http://192.168.1.6:3000/;

}

 

location /bbb/ {

proxy_pass http://192.168.1.6:3001/;

}

}

 

systemctl restart nginx

 

浏览器查看

http://192.168.1.6/aaa/posts

http://192.168.1.6/bbb/comments

 

错误

502 Bad Gateway

解决方案

禁用selinux

setenforce 0

vi /etc/selinux/config

修改

SELINUX=disabled

转载于:https://www.cnblogs.com/xiaobo/p/9358878.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值