我尝试第一次在MAMP中使用Nginx服务器,而不是Apache服务器用于Codeigniter。我将我的htaccess重写规则转换为nginx重写规则。但是它显示的是我文件而非网站上的index.php文件代码。这是我的htaccess规则,
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
这是我的nginx配置文件,
http {
include mime.types;
default_type text/html;
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
sendfile on;
server {
listen 80 default_server;
# MAMP DOCUMENT_ROOT !! Don't remove this line !!
root "C:/MAMP/htdocs/aia_inventory/";
access_log C:/MAMP/logs/nginx_access.log;
error_log C:/MAMP/logs/nginx_error.log;
index index.html index.htm index.php;
#autoindex on;
#server_name localhost;
location ~/ {
#root C:/MAMP/htdocs/aia_inventory/;
#index index.html index.php;
try_files $uri $uri/ /index.php/$request_uri;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
}
location ~* ^/(assets|files|robots\.txt) { }
location ~* /MAMP(.*)$ {
root C:/MAMP/bin;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~* /phpMyAdmin(.*)$ {
root C:/MAMP/bin;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~* /phpLiteAdmin(.*)$ {
root C:/MAMP/bin;
index phpliteadmin.php index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~* /SQLiteManager(.*)$ {
root C:/MAMP/bin;
index index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#location /icons {
# alias /Applications/MAMP/Library/icons;
# autoindex on;
#}
#location /favicon.ico {
# alias /Applications/MAMP/bin/favicon.ico;
# # log_not_found off;
# # access_log off;
#}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#location ~ /\. {
# deny all;
#}
# location ~* \.(gif|jpg|png|pdf)$ {
# expires 30d;
# }
# location = /robots.txt {
# allow all;
# log_not_found off;
# access_log off;
# }
# location ~* \.(txt|log)$ {
# allow 127.0.0.1;
# deny all;
# }
# location ~ \..*/.*\.php$ {
# return 403;
# }
#location /nginx_status {
# stub_status on;
# access_log off;
# allow 127.0.0.1;
# deny all;
#}
}
}
我不知道我在哪里失踪。我的规则适用于apache服务器。但是在nginx服务器上,它仅显示index.php文件原始代码。预先感谢。
答案 0 :(得分:0)
尝试
location ~ / {
root C:/MAMP/htdocs/inventory/;
index index.html index.php;
try_files $uri $uri/ /index.php/$request_uri;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1 break;
}
}
location ~* ^/(assets|files|robots\.txt) { }
答案 1 :(得分:0)
try_files $uri $uri/ /index.php/$request_uri; … location ~ \.php$ {
您遇到的问题是您正在使用http://nginx.org/r/try_files将请求重定向到以/index.php//
开头的URL,而您的.php$
http://nginx.org/r/location处理程序却没有在$uri
部分之后的.php
内根本没有任何期望。
解决方案可能是将$
替换为(/|$)
,例如location ~ \.php(/|$)
。
答案 2 :(得分:0)
使用.htaccess并不是很安全,而是希望在.conf文件中的sites-enable或conf.d目录中进行设置。我建议您这样放置:
sendfile on;
send_timeout 300s;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
# try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone: // in your case is 9100
fastcgi_pass 127.0.0.1: 9100;
# With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.1-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
答案 3 :(得分:0)
您的第一个位置块的定义如下:
location ~/ {
因此,这是包含/
的请求的正则表达式匹配项。您可能会得到很多...
Nginx位置处理首先评估所有指定前缀的位置,并存储最佳匹配项,然后按配置文件中出现的顺序评估正则表达式。如果找到正则表达式匹配项,则它将立即停止进一步查找并选择该位置块来处理请求。
您的第一个位置正则表达式将匹配您收到的每个请求,因此您基本上可以删除配置的其余部分,并且没有任何区别,每个请求都将出现在您的第一个块中。
您的第一个代码块没有fast_cgi指令来将php处理传递给php fpm,因此Nginx会找到该文件并将其仅作为文本存储。
答案 4 :(得分:0)
很抱歉,您的回答很晚。这实际上是非常愚蠢的错误。我的控制器页面名称是小写字母。这就是为什么它不起作用。我的配置还可以。控制器页面的首字母应为大写字母。例如,我的控制器名称是Home。所以我的php文件名必须是Home.php而不是home.php。它将在本地ngincx服务器上运行,但不能在实时服务器上运行。