Nginx configuration, avoid codeigniter rewrite rules

This is the nginx rewrite rule for codeigniter. We can easily find this 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 a regular php project on this server. The project root directory is /var/www/another/proj_new .

As I mentioned, this new project does not use the codeigniter frame. This is a regular php file project. Therefore, the Codeigniter rewrite rule is not required.

So my question is that I can access a new project via the Internet.

the address may be like this:

 http://example.com/proj_new 

This address should not call the codeigniter proj_new controller.

I tried to add this parameter:

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

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

+4
source share
5 answers

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 that, make sure your config.php codeIgniter file contains the following information:

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

Source: Nginx

+4
source

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

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

I assume that you have already tried to create the proj_new folder? It seems to me that you already have sufficient means to achieve what you want in your file, and I do not see any errors. You create your proj_new folder inside the html folder, right?

Just played with it and everything works fine. Your configuration is working properly. Below is my nginx.conf file so you can take a look. It 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; } } } 
+2
source

What version of nginx are you using? This should work with 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; } } 
+1
source

s // try changing this "backend" with your project name

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

can add:

Access to the backend folder

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

Source: https://habr.com/ru/post/1381900/


All Articles