nginx url rewrite for angularjs - php

I am building a website based on angularjs and nginx server.I am struggling at url rewrite for angularjs , I used my development environment as apache which is working well.I want to use extensionless php so that if i visit www.mywebsite.com/register it points to www.mywebsite.com/register.php . When I put slash near www.mywebsite.com/register/it is not working.For angular I want the url as www.mywebsite.com/register/step2 to www.mywebsite.com/register/#!/step2
server {
server_name mywebsite.com;
return 301 $scheme://www.mywebsite.com$request_uri;
}
server {
listen 80;
server_name localhost www.mywebsite.com;
#charset koi8-r;
#access_log /var/log/nginx/log/india.access.log main;
location / {
root /var/www/mywebsite;
index index.php index.html index.htm;
}
location ~ /register/ {
rewrite ^ register.php/#/$1 redirect;
break;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location /api/ {
try_files $uri $uri /api/index.php?$query_string;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
root html;
#include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/mywebsite$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
error_page 404 /404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# if enable status in PHP-FPM config
location ~ ^/(status|ping)$ {
allow 127.0.0.1;
#deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

Related

Nginx how to redirect request for only a certain directory path to index.php in that directory?

I have some websites like
http://localhost/myweb1
http://localhost/myweb2
and so on.
I need to redirect below example request for myweb1 to index.php in http://localhost/myweb1/index.php
while myweb2 should not be affected.
http://localhost/myweb1/user
http://localhost/myweb1/user/account
http://localhost/myweb1/product
and so on should be redirected to myweb1/index.php
Below is my config at /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
location ~ \.php$ {
root /usr/share/nginx/html;
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Answer provided by
#RichardSmith
root /usr/share/nginx/html;
location /myweb1 { try_files $uri $uri/ /myweb1/index.php; }

Nginx rewrite rule for codeigniter for URL parameters

I did the following configurations for nginx server for code igniter and worked correctly , unfortunately there is one case not working.
When I request URL with parameter Like
servername.com/ControllerName/methodName?param=value
I got Not found Page 404
Server Configurations :
server {
# access from localhost only
listen 127.0.0.1:80;
server_name serverName.com;
root API;
log_not_found off;
charset utf-8;
access_log logs/accessservername.log main;
index index.php;
# handle files in the root path /www
location / {
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root API;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9100
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
include fastcgi_params;
}
# add expire headers and speed up image access with a vary header
location ~* ^.+.(gif|ico|jpg|jpeg|png|flv|swf|pdf|mp3|mp4|xml|txt|js|css)$ {
expires 30d;
add_header Vary Accept-Encoding;
}
# only allow these request methods
if ($request_method !~ ^(GET|HEAD|POST)$ ){ return 405; }
}

Codeigniter + Nginx must render 404 page provided by framework

So, I have next problem. This is my nginx config
server {
disable_symlinks off;
listen 80;
server_name g9tv.loc;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root /home/dejmos/www/g9tv;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php;
}
error_page 404 /index.php;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/dejmos/www/g9tv;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors on;
root /home/dejmos/www/g9tv;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/dejmos/www/g9tv$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
When I go to page that not exists on server I got 404 page provided by nginx, but I need to get 404 page provided by codeigniter show_404() function.
Problem resolved. It caused by subfolder. I place Codeigniter to root folder and all ok now.

cannot access file .php inside webroot in cakephp with nginx

I have VPS with this specification:
Ubuntu
Nginx
Php
MySql
So I make project with CakePHP in subfolder named gogon. This project can running and access perfectly. The site url can access at example.com/gogon
problem
I put responsivefilemanager (responsivefilemanager.com) in webroot folder as filemanager, but when I want to access the PHP file in filemanager (dialog.php) site just render my index.php file like example.com/index.php
Structure of my folder:
-example.com
---index.php
---gogon (cakephp project)
-----index.php
-----app
---------config
---------...
---------...
---------...
---------webroot
-------------filemanager
----------------dialog.php
-----lib
-----plugin
-----vendor
This is my nginx config
server {
listen 80;
root /var/www/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ $uri.php /index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location /gogon {
rewrite_log on;
error_log /usr/share/nginx/error.gogon.log error;
if (-f $request_filename) {
break;
}
# Avoid recursivity
if ($request_uri ~ /webroot/index.php) {
break;
}
rewrite ^/gogon$ /gogon/ permanent;
rewrite ^/gogon/app/webroot/(.*) /gogon/app/webroot/index.php?url=$1 last;
rewrite ^/gogon/(.*)$ /gogon/app/webroot/$1 last;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~* \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

How to set up Clean url for Drupal with Nginx in localhost

I setup a nginx server in my localhost
So now I want to enable clean url for this server
(Change localhost/drupalsite/?q=abc to localhost/drupalsite/abc)
I have tried some tutorials but thet don't work for me
http://wiki.nginx.org/Drupal
https://drupal.org/node/976392
http://richardbanks.net/blog/2012/11/nginx-url-rewrites-the-correct-method
This is some basic config in nginx.conf file
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /srv/nginx/www/nginx_htdocs/;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/nginx/www/nginx_htdocs/;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /srv/nginx/www/nginx_htdocs/$fastcgi_script_name;
include fastcgi_params;
}
#--------------- TUNG CUSTOM
#------------- END CUSTOM
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Here's a gist of a full configuration that works very well with Drupal's clean URL's
You must try to handle not found urls as possible query strings for index.php (if that's how Drupal works, which I'm not sure).
For example, in your first location block:
location / {
root /srv/nginx/www/nginx_htdocs/;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$request_uri;
}
This way, if
`http://mysite.com/xyz`
doesn't match any file or folder, it gets handed to index.php in the form of
`http://mysite.com/index.php?xyz`

Categories