摘要:配置主域名二級子域名列表項目其中加入了版本控制,使用的是路由傳入方式在路由文件中配置,如下版本路由省略方法名時有方法名時同時控制器里加入版本號這個例子最終的顯示為項目最終目錄結構為配置主域名配置此處可作反向代理二級接口子域名配置配置解決
配置:
主域名 www.demo.xyz
二級子域名 api.demo.xyz
列表項目其中api.demo.xyz加入了版本控制,使用的是URL路由傳入方式
在route.php路由文件中配置,如下
return [ // api版本路由 ":version/:controller" => ":version.:controller",// 省略方法名時 ":version/:controller/:function" => ":version.:controller/:function"http:// 有方法名時 ];
//同時控制器里加入版本號 namespace appapicontrollerv1; class User { public function login() { $data = ["name"=>"Paul","age"=>19]; return json($data); } }
//這個例子最終的url顯示為http://api.demo.xyz/v1/user Route::get(":version/user", "api/:version.user/login");
項目最終目錄結構為
nginx配置
# 主域名配置 server { listen 80; server_name demo.xyz www.demo.xyz; root /www/www.demo.xyz/dist; #charset koi8-r; #access_log logs/host.access.log main; location / { index index.html index.htm index.php; } # 此處可作反向代理 #location /v1 { # proxy_pass http://api.demo.xyz; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root /www/www.demo.xyz; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache"s document root # concurs with nginx"s one # #location ~ /.ht { # deny all; #} }
# 二級接口子域名配置 server { listen 80; server_name api.demo.xyz; root /www/api.demo.xyz/public; #charset koi8-r; 配置cors解決跨域問題 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods "GET, POST, OPTIONS"; add_header Access-Control-Allow-Headers "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"; if ($request_method = "OPTIONS") { return 204; } #access_log logs/host.access.log main; location / { index index.html index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; } } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ .php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root /www/api.demo.xyz/public; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache"s document root # concurs with nginx"s one # #location ~ /.ht { # deny all; #} }
關于nginx配置cors需要說明的一些問題1.按照CORS on Nginx來配置結果不生效
# # Wide-open CORS config for nginx # location / { if ($request_method = "OPTIONS") { add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; # # Custom headers and headers various browsers *should* be OK with but aren"t # add_header "Access-Control-Allow-Headers" "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"; # # Tell client that this pre-flight info is valid for 20 days # add_header "Access-Control-Max-Age" 1728000; add_header "Content-Type" "text/plain; charset=utf-8"; add_header "Content-Length" 0; return 204; } if ($request_method = "POST") { add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; add_header "Access-Control-Allow-Headers" "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"; add_header "Access-Control-Expose-Headers" "Content-Length,Content-Range"; } if ($request_method = "GET") { add_header "Access-Control-Allow-Origin" "*"; add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS"; add_header "Access-Control-Allow-Headers" "DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"; add_header "Access-Control-Expose-Headers" "Content-Length,Content-Range"; } }2.按照Using CORS來配置成功解決了跨域問題
# 其實就是將下面這段配置放到location塊外面 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods "GET, POST, OPTIONS"; add_header Access-Control-Allow-Headers "DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization"; if ($request_method = "OPTIONS") { return 204; }
服務器最終的目錄結構為
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/29357.html
摘要:配置主域名二級子域名列表項目其中加入了版本控制,使用的是路由傳入方式在路由文件中配置,如下版本路由省略方法名時有方法名時同時控制器里加入版本號這個例子最終的顯示為項目最終目錄結構為配置主域名配置此處可作反向代理二級接口子域名配置配置解決 配置: 主域名 www.demo.xyz 二級子域名 api.demo.xyz 列表項目其中api.demo.xyz加入...
摘要:前后端的界限是按照瀏覽器和服務器的劃分。前后端彼此互不關聯。關于作者本文部分圖片段落參考文章實踐中的前后端分離。淘寶前后端分離實踐本文源碼詳見服務端代碼。 一、起源 (故事純屬虛構,如有雷同,純屬巧合)傳說在很久很久以前,我們有志之士有了個創業的想法,于是乎開始了自己的創業之夢,但是人手不足啊,于是乎所有角色老子一個人全包了: Roles: PM, DBA, RD, FED, Des...
摘要:前后端的界限是按照瀏覽器和服務器的劃分。前后端彼此互不關聯。關于作者本文部分圖片段落參考文章實踐中的前后端分離。淘寶前后端分離實踐本文源碼詳見服務端代碼。 一、起源 (故事純屬虛構,如有雷同,純屬巧合)傳說在很久很久以前,我們有志之士有了個創業的想法,于是乎開始了自己的創業之夢,但是人手不足啊,于是乎所有角色老子一個人全包了: Roles: PM, DBA, RD, FED, Des...
閱讀 3711·2023-04-25 22:43
閱讀 3706·2021-09-06 15:15
閱讀 1332·2019-08-30 15:54
閱讀 3543·2019-08-30 14:20
閱讀 2884·2019-08-29 17:16
閱讀 3117·2019-08-29 15:28
閱讀 3397·2019-08-29 11:08
閱讀 1071·2019-08-28 18:05