需求:微服务 客户端服务器 需要内网访问 api接口服务器
解释:传统的外部网址(比如二级域名)也能实现,但是不安全,于是采用内网域名的形式
实现步骤:
1、nginx解析域名 _innerapi.sd.cn 到 127.0.0.1
$ find / -name hosts
=================
/etc/hosts #这是hosts文件所在的目录
=================
$ vi /etc/hosts
在hosts文件最后一行添加
127.0.0.1 _innerapi.sd.cn
按 ESC
输入 :wq
保存退出
2、添加虚拟主机配置
$ cd /usr/local/nginx/conf/vhost
$ vi _innerapi.sd.cn.conf
输入:
server
{
listen 80;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/api.sd.cn;
include rewrite/thinkphp.conf;
error_page 404 /404.html;
include enable-php-pathinfo.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:8080;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
access_log /home/wwwlogs/_innerapi.squaredental.cn.log;
}
pw: 因为项目是thinkphp5,所以有中间的额外加载项,可以去掉
保存退出,重启nginx:
$ lnmp nginx restart