我有一台 Nginx 服务器 (1.6),本地服务器上有 PHP-FPM。我的“/var/www/”文件夹中有多个 webapps,例如 site01、site02...
我的应用程序使用 CodeIgniter。
我想在默认位置调用 /var/www/site01,并在我去时调用 /var/www/site02:
http://server/site02.
这是我的 nginx default.conf:
服务器{监听 80;服务器名称 localhost;access_log 关闭;客户端主体缓冲区大小 1M;代理最大临时文件大小 0;disable_symlinks 关闭;
# Site 01
location / {
root /var/www/site01;
index index.html index.php index.htm;
try_files $uri $uri/ /index.php;
location = /index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 10000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16K;
fastcgi_buffers 256 16k;
include fastcgi_params;
}
}
# Site 2
location /site02 {
root /var/www/;
index index.htm;
try_files $uri $uri/ /index.php;
location ~ ^/site02/index.php {
try_files $uri =404;
root /var/www/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 10000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16K;
fastcgi_buffers 256 16k;
include /etc/nginx/fastcgi_params;
}
}
我遇到了多个错误,首先,当我没有在 site02 URL 中输入“index.php”时,出现了 403 错误。然后,我无法访问任何控制器,它会将我重定向到 /site01。
处理多个 PHP 应用程序文件夹的最佳方法是什么?为什么我有这些重定向?谢谢。
答案1
首先我看到你的第二个站点配置:
location /site02 {
root /var/www/;
index index.htm;
改成 :
location /site02 {
root /var/www/;
index index.php index.htm;
需要index.php
答案2
当我在一个 Nginx 服务器上开发 2 个 CodeIgniter 应用程序时,我也遇到了同样的问题。下面的代码对我有用
server {
listen 80;
listen [::]:80;
root /var/www/html/;
server_name example.com;
# Site 01
location /site1 {
alias /var/www/html/site1/;
index index.htm index.php;
try_files $uri $uri/ /site1/index.php;
}
# Site 2
location /site2 {
alias /var/www/html/site2/;
index index.htm index.php;
try_files $uri $uri/ /site2/index.php;
}
error_page 404 /error/404.php;
fastcgi_intercept_errors on;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
答案3
好吧,很难解决!但这对我有用:
location /site02 {
root /var/www/;
index index.php index.htm;
try_files $uri $uri/ /site02/index.php;
location ~ ^/site02/(.+\.php)$ {
root /var/www;
try_files $uri $uri/ /site02/index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 10000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffer_size 16K;
fastcgi_buffers 256 16k;
include fastcgi_params;
}
}
答案4
server {
listen 80;
server_name themainwebsite.test *.themainwebsite.test;
root "C:/laragon/www/themainwebsite";
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
autoindex on;
}
# Site 2
location /admin-dashboard{
alias "C:/laragon/www/borneotropicalstream/admin-dashboard";
index index.php index.htm ;
try_files $uri $uri/ /admin-dashboard/index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass php_upstream;
#fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
# deny the .htaccess file if exist
location ~ /\.ht {
deny all;
}
location ~ /admin-dashboard\.ht {
deny all;
}
}
这是我的配置。我将我的管理页面(使用 Codeigniter v3 构建)放在 codeigniter v3 主项目中。我使用的是 Windows 10 并使用 Laragon (https://laragon.org) 使用 PHP v7.4 和 Nginx v1.22.0 进行 Web 服务器管理。我认为这种配置可以在其他操作系统中实现。