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

posted @ 2018-07-24 11:09  抱影无眠  阅读(326)  评论(0编辑  收藏  举报