Nginx
Nginx:將子域重寫到其中的文件夾和文件
我有 nginx 重寫規則-它將所有子域請求從 sub 重定向到文件夾:
server { listen 77.77.77.77:80; server_name domaincom *.domaincom; root /home/admin/web/domaincom/public_html/subs/$subdomain; set $subdomain ""; if ($host ~* ^([a-z0-9-\.]+)\.domaincom$) { set $subdomain $1; #rewrite $request_uri $1.$request_uri; #rewrite ^(.*txt)$ $subdomain.$request_uri; just testing here #rewrite ^(.*\.txt)$ $subdomain.$request_uri/$1; } if ($host ~* ^www.domaincom$) { set $subdomain ""; } # location $subdomain { # # # An example rewrite # rewrite ^/(.*txt)$ $request_uri; # } index index.php; access_log off; error_log /dev/null; location / { root /home/admin/web/domaincom/public_html; location ~* ^.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ { expires max; } location ~ [^/]\.php(/|$) { fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; if (!-f $document_root$fastcgi_script_name) { return 404; } fastcgi_pass 127.0.0.1:9002; fastcgi_index index.php; include /etc/nginx/fastcgi_params; } } error_page 403 /error/404.html; error_page 404 = @foobar; location @foobar { return 301 /; } error_page 500 502 503 504 /error/50x.html; location /error/ { alias /home/admin/web/domaincom/document_errors/; } # Access deny for bots # See bots list in the /etc/nginx/nginx.conf if ($limit_bots = 1) { return 403; } location ~* "/\.(htaccess|htpasswd)$" { deny all; return 404; } location /vstats/ { alias /home/admin/web/domaincom/stats/; include /home/admin/conf/web/domaincom.auth*; } include /etc/nginx/conf.d/phpmyadmin.inc*; include /etc/nginx/conf.d/phppgadmin.inc*; include /etc/nginx/conf.d/webmail.inc*; include /home/admin/conf/web/nginx.domaincom.conf*; }
它運作良好,但我有一個問題。
在域和子域中,我有一個相同的 php 腳本,可以從 txt 文件中獲取數據,如下所示:
file(key.txt);
我認為,我的 php-fpm 模組不了解 nginx 規則並從 ROOT 域獲取 SUB 中的數據 - 這是錯誤的。請幫我添加 nginx 例外或添加規則以從 SUB 獲取 SUB 中的 txt 數據。Not_from_root_domain。謝謝。
您的配置的一些優化:
map
1)在指令的幫助下定義您的子域根:map $http_host $root_suffix { ~^(?:www\.)?domain\.com$ ""; ~^(.+)\.domain\.com$ /subs/$1; }
2)根據請求URI後綴定義過期時間:
map $uri $expires { default off; ~*.+\.(jpeg|jpg|png|gif|bmp|ico|svg|css|js)$ max; }
(此
map
指令必須在塊外部server { }
,在http { }
上下文級別定義。)現在你的
server { }
塊:server { listen 77.77.77.77:80; server_name .domaincom; root /home/admin/web/domaincom/public_html$root_suffix; expires $expires; index index.php; access_log off; error_log /dev/null; # your locations here location ~ [^/]\.php(/|$) { ... } ...