php-主要CodeIgniter应用子目录中CodeIgniter应用的Nginx重写配置

前端之家收集整理的这篇文章主要介绍了php-主要CodeIgniter应用子目录中CodeIgniter应用的Nginx重写配置 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

从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;
    }
}
原文链接:https://www.f2er.com/nginx/532426.html

猜你在找的Nginx相关文章