从Apache迁移到新的Nginx服务器,我不知道这个新的重写代码…
我有一个拥有主要CodeIgniter应用程序的网站,比方说www.codeigniterapp.com.
然后我有单独的CodeIgniter应用程序,例如www.codeigniterapp.com/random-directory/app2等.
Main CodeIgniter应用程序可以正常工作,但加载基本URL以外的任何内容时,子目录中的应用程序将失败.
例如,www.codeigniterapp.com/random-directory/app2可以工作,但是www.codeigniterapp.com/random-directory/app2/some-action-此处将打印出404错误.
我需要一些操作,将其发送到index.php.
它是这样的:
www.codeigniterapp.com/random-directory/app2/index.php?/some-action-here
因此,在创建Nginx重写代码时需要帮助.
app2-目录中的旧apache .htaccess如下所示:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /random-directory/app2/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
现在,我正在使用像这样的Nginx(优化代码点火器)虚拟主机配置:
server {
listen 80;
server_name codeigniterapp.com www.codeigniterapp.com;
root /var/www/codeigniterapp.com;
add_header Strict-Transport-Security max-age=15768000;
add_header X-UA-Compatible "IE=Edge,chrome=1";
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "NOSNIFF";
autoindex off;
index maintenance.html index.php index.html index.htm;
# Hide files that begin with a "."
location ~ /\. {
access_log off;
log_not_found off;
return 404;
}
# Images and text files are served normally, while everything else is downloaded.
# PHP will not execute!
location /uploads {
types {
image/gif gif;
image/jpeg jpeg jpg;
image/png png;
text/plain txt;
application/pdf pdf;
}
default_type application/octet-stream;
location ~ \.php$
{
break;
}
}
# Codeigniter; rewrite default controller and "/index.php" back to root domain.
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$) {
rewrite ^(.*)$/ permanent;
}
# Codeigniter; rewrite "controller/index" back to root "controller".
if ($request_uri ~* index/?$) {
rewrite ^/(.*)/index/?$/$1 permanent;
}
# Codeigniter; remove trailing slashes.
if (!-d $request_filename) {
rewrite ^/(.+)/$/$1 permanent;
}
# Main location block. If the file or folder exists, display it, otherwise fallback
# to the site location block.
location / {
try_files $uri $uri/ @site;
}
# Codeigniter; pass the current request to the index.php file for Codeigniter to handle.
location @site {
rewrite ^/(.*)$/index.php?/$1 last;
}
# Proxy PHP files to PHP-FPM.
# If the file doesn't exist then throw a 404 (defined below).
location ~ \.php${
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param CI_ENV production;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
# Pass not-found errors back to Codeigniter to handle.
error_page 404 /index.php;
# Cache static files. Don't bother logging them.
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)${
access_log off;
log_not_found off;
expires 30d;
}
}
解决方法:
问题解决了!我在vhost配置中添加了以下内容
location /random-directory/app2/ {
if ($request_uri ~ "^system.*"){
rewrite ^/random-directory/app2/(.*)$/random-directory/app2/index.php?/$1 last;
}
if (!-e $request_filename){
rewrite ^/random-directory/app2/(.*)$/random-directory/app2/index.php?/$1 last;
}
}