Nginx rewrite rule for CodeIgniter

前端 未结 3 1488
抹茶落季
抹茶落季 2021-02-03 14:09

Here is the rule in English:

Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your

相关标签:
3条回答
  • 2021-02-03 14:39

    You can add this to your config:

    location ~* ^/(assets|files|robots\.txt) { }
    

    This will work correctly with your location / rule.

    Your config also need to add root document and default index file.

    ...
    root /ftp/wardrobe;
    index index.php index.html index.htm;
    
    location / {
        try_files $uri $uri/ /index.php?/$request_uri;
    }
    
    location ~* ^/(assets|files|robots\.txt) { }
    ...
    
    0 讨论(0)
  • 2021-02-03 14:40

    Please try this. It works for me.

    server {
        server_name domain.tld;
    
        root /var/www/codeignitor;
        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 or directory index file exists, else route it to index.php.
            try_files $uri $uri/ /index.php;
        }
    
        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 SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    
    0 讨论(0)
  • 2021-02-03 15:01

    You want to do this instead:

    if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
        rewrite ^/(.*)$ /index.php/$1 last;
    }
    

    $request_uri is for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.

    Also, you need to escape special regular expression characters like . by using a backslash.

    0 讨论(0)
提交回复
热议问题