简体   繁体   English

nginx配置,避免codeigniter重写规则

[英]nginx configuration, avoid codeigniter rewrite rules

This is nginx rewrite rule for codeigniter. 这是codeigniter的nginx重写规则。 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. 我想在这个服务器中添加正常的php项目。 The project's root directory is /var/www/another/proj_new . 项目的根目录是/var/www/another/proj_new

As I mentioned, this new project do not use codeigniter frame. 正如我所提到的,这个新项目不使用codeigniter框架。 This is normal php file project. 这是正常的php文件项目。 So it doesn't need Codeigniter rewrite rule. 所以它不需要Codeigniter重写规则。

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. 该地址不应该调用codeigniter的proj_new控制器。

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. 但是, http://example.com/proj_new会生成404错误页面。

I suggest this configuration from Nginx 我建议从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: 在此之后,请确保您的codeIgniter config.php包含以下信息:

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

Source: Nginx 资料来源: Nginx

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. 以下代码部分中的!-e表示如果文件,目录或符号链接不存在,则重定向以使用重写规则。 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. 你有这个存在的事实应该足以让你只创建一个文件夹proj_new并且应该忽略重写规则。

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

I presume you have tried just creating the proj_new folder already? proj_new你已经尝试过创建proj_new文件夹了吗? 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? 你正在html文件夹中创建你的proj_new文件夹,对吗?

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. 下面是我的nginx.conf文件,所以你可以看看。 This was CI2.1, Nginx 1.0.1 Stable, Windows 7, PHP 5.3.1. 这是CI2.1,Nginx 1.0.1稳定,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;
        }
    }
}

What version of nginx are you using? 您使用的是什么版本的nginx? This should work on newer versions, with the try_files directive. 这应该适用于较新的版本,使用try_files指令。

http://ericlbarnes.com/post/12197460552/codeigniter-nginx-virtual-host 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;
    }
}

s// try this change the "backend" with your projectname s //尝试使用您的项目名称更改“后端”

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;
}

can you add: 你能补充一下:

Can access to "backend" folder 可以访问“后端”文件夹

    if ($request_uri ~* ^/backend)
    {
        rewrite ^/(.*)$ /backend/index.php?/$1 last;
        break;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM