CentOS7安裝Nginx實現API網關

參考

http://nginx.org/ html

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

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

 

安裝

yum install openssl zlib pcre npm

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

yum install nginx vim

 

命令行啓動

nginx –h centos

nginx api

nginx -s stop 瀏覽器

 

服務模式

systemctl start nginx post

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

相關文章
相關標籤/搜索