[英]nginx rewrite rules for Codeigniter are not working correctly
我第一次嘗試在MAMP中使用nginx服務器而不是用於Codeigniter的apache服務器。 我將我的apache htaccess重寫規則轉換為nginx重寫規則。 但它顯示的是我文件中的index.php文件代碼而不是我的網站。 這是我的apache 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文件的原始代碼。 提前致謝。
試試這個
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) { }
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處理程序不希望在.php
部分之后的$uri
有任何內容。
解決方案可能是用$
替換$
(/|$)
,例如, location ~ \\.php(/|$)
。
使用.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;
}
您的第一個位置塊定義如下:
location ~/ {
所以這是包含/
請求的正則表達式匹配。 你可能會得到很多......
Nginx位置處理首先評估所有前綴指定的位置並存儲最佳匹配,然后按配置文件中的外觀順序計算正則表達式。 如果它找到正則表達式匹配,則它會立即停止查找,並選擇該位置塊來處理請求。
您的第一個位置正則表達式將匹配您獲得的每個請求,因此您基本上可以刪除配置的其余部分,這將沒有任何區別,每個請求將最終在您的第一個塊中。
你的第一個塊沒有fast_cgi指令將php處理傳遞給php fpm,所以Nginx找到文件並將其作為文本服務。
抱歉回復晚了。 這實際上是非常愚蠢的錯誤。 我的控制器頁面名稱是小字符。 這就是它無法正常工作的原因。 我的配置沒問題。 控制器頁面的第一個字母應為大寫字母。 例如,我的控制器名稱是Home。 所以我的php文件名必須是Home.php而不是home.php。 它可以在本地ngincx服務器上運行,但不能在實時服務器上運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.