nginx配置接口多版本,不同版本指向不同目录
微wx笑
2019-10-14【网络工具】
10
0关键字:
Nginx
一个域名下面配置多个版本接口
版本一
路径/data/a/b
版本二
路径/data/c/d
版本三
路径/data/e/f
nginx配置接口多版本,不同版本指向不同目录
一个域名下面配置多个版本接口
版本一
路径/data/a/b
版本二
路径/data/c/d
版本三
路径/data/e/f
server {
listen 80;
server_name localhost;
charset utf-8;
access_log off;
location /v1/ {
alias /data/a/b/;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/v1/(.*)$ /index.php/$1 last;
}
location ~ \.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
location /v2/ {
alias /data/c/d/;
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^/v2/(.*)$ /index.php/$1 last;
}
location ~ \.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
location ~ /\.ht {
deny all;
}
}注意事项:
location /v1/ { alias /data/a/b/;
实际解析的PHP路径为:/data/a/b/v1/
也就是说你访问是的 https://www.ivu4e.com/v1/login.php
nginx 会去找路径为 /data/a/b/v1/login.php 的文件
如果你访问的是 https://www.ivu4e.com/v1/login.txt
nginx 会去找路径为 /data/a/b/login.txt 的文件
Windows 下路径中的“\”要改为“/”, alias 和 root 效果其实是一样的;
但Windows下需要将
fastcgi_pass unix:/dev/shm/php-fpm.sock;
替换为:
fastcgi_pass 127.0.0.1:9123;
端口根据你的配置修改。
参考:https://segmentfault.com/q/1010000016038182
本文为转载文章,版权归原作者所有,不代表本站立场和观点。



