我花了很多时间进行看似简单的配置,但我不知道正确的配置:(
我有 2 个不同的应用程序,它们必须提供 2 个不同的 URL。这两个应用程序下的所有内容都需要捕获所有 index.php。
示例:(公共 URL => 服务路径(应用程序))
https://example.com/ => /var/public/index.php
https://example.com/xy => /var/public/index.php
https://example.com/xy/zw => /var/public/index.php
但!
https://example.com/api/v2 => /api/public/index.php
https://example.com/api/v2/xy => /api/public/index.php
https://example.com/api/v2/xy/test => /api/public/index.php
我还配置了 Apache 反向代理:(仅用于将 /api/v2 重写为 /api/public,因此在 nginx 上仅显示 /api/public uri - 需要反向代理,我只有 1 个公共 IP 但有很多站点)
<VirtualHost *:443>
ServerName xxx.com
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
SSLEngine On
SSLCertificateFile /etc/apache2/cert/xxx.com.pem
SSLCertificateKeyFile /etc/apache2/cert/xxx.com.key
SSLCertificateChainFile /etc/apache2/cert/yyyy.pem
ProxyRequests Off
ProxyPreserveHost On
RewriteEngine On
RewriteRule ^/api/v2$ /api/v2/ [R,L]
<Location /api/v2/>
ProxyPass http://1.2.3.4/api/public/ KeepAlive=On TimeOut=3600 retry=0
ProxyPassReverse http://1.2.3.4/api/public/
</Location>
ProxyPass / http://1.2.3.4/ KeepAlive=On TimeOut=3600 retry=0
ProxyPassReverse / http://1.2.3.4/
LogLevel debug
ErrorLog /var/log/apache2/xxx-web-error.log
CustomLog /var/log/apache2/xxx-web-access.log common
</VirtualHost>
我的配置不适用于第二部分,只适用于一个精确路径:https://example.com/api/v2。
在没有 fastcgi php 执行的情况下,我管理了这个配置,但这不适用于动态 php。
我的 nginx 配置:
server {
listen 80;
#server_name localhost;
# main root for GUI
#root /api/public;
root /var/public;
index index.php;
location /api/public {
alias /api/public;
try_files $uri $uri/ /api/public/index.php?$args;
#try_files $uri $uri/ /api/public/index.html;
location ~ \.php$ {
include fastcgi_params;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
}
location / {
try_files $uri $uri/ /index.php?$args;
#try_files $uri $uri/ /index.html;
location ~ \.php$ {
include fastcgi_params;
try_files $uri = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
access_log /var/log/nginx/scripts.log scripts;
error_log /var/log/nginx/error.log debug;
}
有什么建议吗?
谢谢!!!
更新 index.php 文件的内容:
<html>
<body>
<?php
print "<pre>";
print_r($_SERVER);
print "</pre>";
?>
</body>
</html>
答案1
解决了!
我变了:
location /api/public
=>location ~ ^/api/public
正在
location /api/public
改变try_files $uri $uri/ /api/public/index.php?$args;
到
try_files $uri $uri/ /index.php?$args;
最重要的!修改
location /
为:location / { try_files $uri $uri/ /index.php?$args; #try_files $uri $uri/ /index.html; location ~ \.php$ { set $php_root $document_root; if ($request_uri ~ ^/api/public) { set $php_root /api/public; } include fastcgi_params; try_files $uri = 404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name; } }
加块为:
set $php_root $document_root;
if ($request_uri ~ ^/api/public) {
set $php_root /api/public;
}
当然可以$document_root
改为$php_root