nginx configuration, avoid codeigniter rewrite rul

2019-05-06 06:59发布

This is nginx rewrite rule for codeigniter. We can find this easily with googling.

server
{
    server_name .example.com;

    access_log /var/log/nginx/example.com.access.log;

    root /var/www/example.com/html;

    index index.php index.html index.htm;

    # enforce www (exclude certain subdomains)
#   if ($host !~* ^(www|subdomain))
#   {
#       rewrite ^/(.*)$ $scheme://www.$host/$1 permanent;
#   }

    # enforce NO www
    if ($host ~* ^www\.(.*))
    {
        set $host_without_www $1;
        rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
    }

    # canonicalize codeigniter url end points
    # if your default controller is something other than "welcome" you should change the following
    if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
    {
        rewrite ^(.*)$ / permanent;
    }

    # removes trailing "index" from all controllers
    if ($request_uri ~* index/?$)
    {
        rewrite ^/(.*)/index/?$ /$1 permanent;
    }

    # removes trailing slashes (prevents SEO duplicate content issues)
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ /$1 permanent;
    }

    # removes access to "system" folder, also allows a "System.php" controller
    if ($request_uri ~* ^/system)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/$1 last;
        break;
    }

    # catch all
    error_page 404 /index.php;

    # use fastcgi for all php files
    location ~ \.php$
    {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/example.com/html$fastcgi_script_name;
        include fastcgi_params;
    }

    # deny access to apache .htaccess files
    location ~ /\.ht
    {
        deny all;
    }
}

I want to add normal php project in this server. The project's root directory is /var/www/another/proj_new.

As I mentioned, this new project do not use codeigniter frame. This is normal php file project. So it doesn't need Codeigniter rewrite rule.

So, my question is is possible that I can access the new project through the web.

the address may be like this:

http://example.com/proj_new

This address should not call codeigniter's proj_new controller.

I've tried to add this setting :

server
    {
        ....
        ....
        ....

        localhost /proj_new {
            root     /var/www/another/proj_new
            index    index.php
        }

        ....
        ....
        ....
    }

but, http://example.com/proj_new makes 404 error pages.

5条回答
孤傲高冷的网名
2楼-- · 2019-05-06 07:21

can you add:

Can access to "backend" folder

    if ($request_uri ~* ^/backend)
    {
        rewrite ^/(.*)$ /backend/index.php?/$1 last;
        break;
    }
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-06 07:27

s// try this change the "backend" with your projectname

location / {
    # Check if a file or directory index file exists, else route it to index.php.
    try_files $uri $uri/ /index.php;
}

# canonicalize url end points
# if your default controller is something other than "welcome" you should change the following
if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
{
    rewrite ^/backend/(.*)$ /backend/ permanent;
}

# removes trailing "index" from all controllers
if ($request_uri ~* index/?$)
{
    rewrite ^/backend/(.*)/index/?$ /backend/$1 permanent;
}

# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
    rewrite ^/backend/(.+)/$ /backend/$1 permanent;
}

# removes access to "system" folder
if ($request_uri ~* ^/system)
{
    rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
    break;
}

# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
    rewrite ^/backend/(.*)$ /backend/index.php?/$1 last;
    break;
}
查看更多
老娘就宠你
4楼-- · 2019-05-06 07:28

I suggest this configuration from Nginx

server {
        server_name nginxcodeigniter.net;

        root /var/www/codeigniter;
        index index.html index.php;

        # set expiration of assets to MAX for caching
        location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
                expires max;
                log_not_found off;
        }

        location / {
                # Check if a file exists, or route it to index.php.
                try_files $uri $uri/ /index.php;
        }

        location ~* \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

After this, make sure that your codeIgniter config.php contains the following information:

$config['base_url'] = "http://nginxcodeigniter.net/";
$config['index_page']   = "";
$config['uri_protocol'] = "REQUEST_URI";

Source: Nginx

查看更多
放荡不羁爱自由
5楼-- · 2019-05-06 07:34

What version of nginx are you using? This should work on newer versions, with the try_files directive.

http://ericlbarnes.com/post/12197460552/codeigniter-nginx-virtual-host

server {
    server_name .mysecondsite.com;
    root /sites/secondpath/www;

    index index.html index.php index.htm;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

    location / {
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}
查看更多
Lonely孤独者°
6楼-- · 2019-05-06 07:38

The !-e in the following section of code means that if the file, directory or symlink does not exist, redirect to use the rewrite rule. The fact that you have this present should be enough for you to just create a folder proj_new and the rewrite rule should be ignored.

if (!-e $request_filename)
{
    rewrite ^/(.*)$ /index.php?/$1 last;
    break;
}

I presume you have tried just creating the proj_new folder already? It looks to me as if you already have sufficient means to achieve what you want in your file and I can't see any errors with it. You are creating your proj_new folder inside the html folder, right?

Just had a play about with this and it works fine. Your configuration works as expected. Attached below is my nginx.conf file so you can have a look. This was CI2.1, Nginx 1.0.1 Stable, Windows 7, PHP 5.3.1.

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        index index.php index.html index.htm;

        # enforce NO www
        if ($host ~* ^www\.(.*))
        {
            set $host_without_www $1;
            rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
        }

        # canonicalize codeigniter url end points
        # if your default controller is something other than "welcome" you should change the following
        if ($request_uri ~* ^(/welcome(/index)?|/index(.php)?)/?$)
        {
            rewrite ^(.*)$ / permanent;
        }

        # removes trailing "index" from all controllers
        if ($request_uri ~* index/?$)
        {
            rewrite ^/(.*)/index/?$ /$1 permanent;
        }

        # removes trailing slashes (prevents SEO duplicate content issues)
        if (!-d $request_filename)
        {
            rewrite ^/(.+)/$ /$1 permanent;
        }

        # removes access to "system" folder, also allows a "System.php" controller
        if ($request_uri ~* ^/system)
        {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
        }

        # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
        if (!-e $request_filename)
        {
            rewrite ^/(.*)$ /index.php?/$1 last;
            break;
        }

        # catch all
        error_page 404 /index.php;

        # use fastcgi for all php files
        location ~ \.php$
        {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # deny access to apache .htaccess files
        location ~ /\.ht
        {
            deny all;
        }
    }
}
查看更多
登录 后发表回答