Rewrite rule in nginx webserver - php

I still struggling with make rewrite rule in my webserver. When I tryin login (www.example.com/admin/) to my backend I have only white page. But If I try to add index (www.example.com/admin/index) or index.php (www.example.com/admin/index.php) my backend is working. So how can I edit code for function www.example.com/admin/ ?
Thanks a lot for any help!
There is my code:
server_names_hash_bucket_size 64;
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name www.example.com;
location /admin/ {
rewrite ^ /admin/index.php;
}
location / {
rewrite ^([^\.]*)$ /$1.php;
}
location = / {
rewrite ^ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

Try to add this:
location /admin/ {
rewrite ^ /admin/index.php;
}

Related

Nginx rewrite rules are not working as expected

I have a website
https://dev.mywebsite.com/index.php?album=portraits
which displays photo-albums dynamically based on a POST value.
I want to rewrite this URL to this:
https://dev.mywebsite.com/portraits
But my Nginx rewrite rule is not working. Nothing is happening when I enter https://dev.mywebsite.com/index.php?album=portraits. And no page is found when entering https://dev.mywebsite.com/portraits.
I don't know what I am doing wrong.
This is the code I'm trying to use currently:
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
I've also tried this:
location = /portraits {
rewrite ^/portraits?$ /index.php?album=portraits break;
}
and this:
location = /album {
rewrite ^album/([a-z]+)/?$ album.php?album=$1 break;
}
This is the entire nginx site-config file i'm using:
server {
listen 80 default_server;
listen 443 ssl;
root /config/www;
index index.php index.htm index.html;
server_name dev.mywebsite.com;
ssl_certificate /config/keys/cert.crt;
ssl_certificate_key /config/keys/cert.key;
client_max_body_size 0;
location / {
try_files $uri $uri/ /index.html /index.php?$args =404;
}
location / {
rewrite ^/(.*)$ /album.php?album=$1 last;
}
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
If the file you really have is index.php, then you can rewrite (without any additional location) with:
rewrite ^/portraits$ /index.php?album=portraits last;
Keep the location ~ \.php$ { of course :)

Want to redirect old domain to new domain using nginx but not working

I have set the two Sub domain for front end and back end code. I want to redirect the new domain name if user hit the old domain. But I am getting error- "414 Request-URI Too Large". I am using Nginx server for back end code.
server {
root /var/www/html/frontendCode/dist;
index index.php index.html index.htm;
server_name dev.olddomain.com;
location / {
try_files $uri $uri/ /index.html;
}
rewrite ^ $scheme://dev.newdomain.com$request_uri permanent;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name devapi.olddomain.com;
root /var/www/html/apiBackendCode/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
rewrite ^ $scheme://devapi.newdomain.com$request_uri permanent;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
}
}
you can redirect using this:
server {
listen 80;
server_name dev.domain1.com;
rewrite ^/(.*) https://dev.domain2.com/$1 permanent;
}
Try this
server {
server_name .mydomain.com;
rewrite ^ http://www.adifferentdomain.com$request_uri? permanent;
}
you can try redirecting like that.
return 301 http://dev.newdomain.com$request_uri;

Nginx conf for codeigniter result 404

I have setup my nginx conf following this link for my codeigniter server block
https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
with some addition like this
server {
listen 80;
server_name domain.com;
autoindex on;
root /var/www/server;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
location = /index.php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/server$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
location ~* ^/(assets|files|robots\.txt) { }
location ~ /\.ht {
deny all;
}
}
But turns out still routed to error_404 default by CI. Anybody could enlighten me? Thanks
it's resolved, no need for nginx conf. Merely from small mistakes with filename

Nginx + Codeigniter rewrite

after i move my app to Nginx server things just blow up.
My site is loaded, but when i try to access some controller return 404 not found.
Read alot of articles how to configure nginx.conf but without success.
Here is my nginx.conf
server {
listen 80;
server_name example.com;
root /var/www/html/travel;
index index.php;
# Enable rewrite error log
error_log /var/log/nginx/travel.error_log debug;
rewrite_log on;
# Any HTTP request other than those for assets folder, files folder and robots.txt
# is treated as a request for your index.php file.
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
location ~* ^/(assets|files|robots\.txt) { }
# Deny access to .htaccess files, if Apache's document root
# concurs with Nginx's one
location ~ /\.ht {
deny all;
}
}
Strange thing is that in error.log has no errors.
Here is mine Codeigniter config.php
$config['base_url'] = 'http://example.com';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
Check if this helps. Replace both occurrences of <source_directory>; with your correct value:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root <source_directory>;
rewrite_log on;
index index.html index.php;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
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;
}
# use fastcgi for all php files
location ~ \.php$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME <source_directory>$fastcgi_script_name;
include fastcgi_params;
}
# deny access to apache .htaccess files
location ~ /\.htaccess
{
deny all;
}
error_page 404 /404.html;
location = /40x.html
{
}
error_page 500 502 503 504 /50x.html;
location = /50x.html
{
}
}
server {
server_name domain.tld;
root /var/www/html/travel;
index 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$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
This is my config with some changes.

remove .php from url with rewrite rule

I want to rewrite the url in nginx so that the .php extension can be omitted.
This is what I have, but this isn't working for me. Anyone any idea how to do this?
Thanks.
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name www.example.com;
error_page 404 http://www.example.com/404.php;
autoindex off;
error_log /usr/share/nginx/www/nginx_error.log warn;
location / {
rewrite ^(.*)$ /$1.php;
}
location = / {
rewrite ^ /index.php;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
this is my whole config file.
Spent a while trying to get this to work properly. Both php files and other assets working:
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /$1.php;
}
try_files $uri $uri/ /index.php?$query_string;
}
#Makromat I think the 404 issue you are mentioning in the comments is that your / location is resolving to /.php without a file name, so to solve it we might add another location just to handle this special case
location / {
rewrite ^(.*)$ /$1.php;
}
location = / {
rewrite ^ /index.php;
}
Try this and tell me if it works
EDIT:
Ok scratch that, I have a better idea, the first case will fail if the original URL already contains the .php extension so it's better to define it as a fall back solution, try this
location / {
try_files $uri $uri.php $uri/;
}
Try replace this with your current php block
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
Something like this should be what you are looking for.
location ^/(.*)$ {
rewrite ^(.*)$ /$1.php;
}

Categories