【发布时间】:2023-03-12 13:42:02
【问题描述】:
从 Apache 迁移到新的 Nginx 服务器我无法弄清楚这个新的重写代码...
我有一个包含主要 CodeIgniter 应用程序的网站,比如说 www.codeigniterapp.com。
然后我有单独的 CodeIgniter 应用程序,例如 www.codeigniterapp.com/random-directory/app2 等。
主要的 CodeIgniter 应用程序工作正常,但子目录中的应用程序在加载除基本 url 之外的任何内容时会失败。
例如 www.codeigniterapp.com/random-directory/app2 有效,但 www.codeigniterapp.com/random-directory/app2/some-action-here将打印出 404 错误。 我需要将 some-action-here 发送到 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(Code Igniter 优化)vhost 配置:
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;
}
}
【问题讨论】:
标签: php apache .htaccess codeigniter nginx