Configring Nginx for rest api - php

I have implemented Rest Api(Post) in apache server but when i moved it to nginx it not working. Here is configigration file i used in root directory called .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ api.php [QSA,NC,L]
</IfModule>
All it does is if a request comes to ipaddess of my server like example.com/promocode_api/validate it sends request to api.php to process it.
But cant able to get configration needed in nginx to make it to work.
Here is my nginx code from etc/nginx/sites-enabled/defalut.
server {
listen 80;
root /purple_dev;
index index.php index.html index.htm;
server_name example.com;
# location / {
# try_files $uri $uri/ /index.html;
# }
location /promocode_api {
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /purple_dev;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

You'll probably need somthing along the lines of:
location /promocode_api {
index api.php;
include mime.types;
try_files $uri $uri/ api.php$is_args$args;
}
I fail to get the point why you have 3 different rules for the same target file though. I would deal with these in php.

Finally i solved it.
This was neede to be added
location /promocode_api {
rewrite ^/promocode_api/(.*)$ /promocode_api/api.php?rquest=$1;
}

Related

Lumen Route redirect to 404 in Nginx

I am doing one project in lumen, and I have installed this in my LAMP server. I have use a htaccess file to strip index.php from url.
here is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Here everything is working fine.
Now I am moving my files to newly created instance with LEMP stack server (nginx). This is lumen file, so I have installed composer in my project directory.
when I am putting a test route in url (browser), (eg: website.com/getUser) it is showing 404 page error.
SO I have modified the nginx default file in /etc/nginx/sites-available/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
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;
include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
and then I have created a separate server block file for my website by copying the default file and making some changes. that file is below.
server {
listen 80 ;
listen [::]:80 ;
root /var/www/html/my.website.com;
server_name my.website.com;
location / {
rewrite ^/(.*)/$ /$1 redirect;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Now, when I typing my url (website.com/getUser), then it is downloading something. after opening in sublime, I get to know that this file is actually index.php which is in root directory of my project (website.com/index.php)
I am not getting, why it is happening. Why I am not be able to access my route. Where is the problem, can you guys help me in this.
Thanks in advance.
Add this to your config:
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Nginx config for pretty URLs

Previously I was working with Apache server (I had a special .htaccess) for my router, and everything worked as expected:
request: test.local/index/action was handled by test.local/index.php.
My .htaccess file was:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
The problem: Now, I need to implement same functionality in Nginx. I implement nginx configs for my websites including in main nginx.conf -nginx folder- /include.d/test.conf . I tried this test.conf configuration:
server {
listen 80;
listen [::]:80;
server_name productlocator.local;
root /var/www/test/;
index index.php index.html;
location ~ \.php$ {
# fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $uri $uri/ /index.php?$query_string;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
Requests to test.local/index.php are properly handled, but requests like test.local/index/action result with 404 Not Found error.
The question: How do I configure Nginx for my router to work?
Use
server_name test.local;
instead
server_name productlocator.local;
And then look this link

Problems moving Sites from Apache to Nginx (mod_rewrite)

I am migrating some vhosts from apache to nginx. Unfortunately i am having some troubles because of .htaccess files from the apache instance.
Here is the situation (apache):
The DocumentRoot has different folders with projects inside. I could access most of them (through www.example.com/appname) without problems.
But on accessing (the main page) www.example.com the following .htaccess file redirects the users.
/var/www/html/.htaccess:
Redirect /index.html https://example.com/example/
Which then has another .htaccess in /var/www/html/example/.htaccess
Options -Indexes
IndexIgnore */*
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/$1
So then the next .htaccess in /var/www/html/example/web
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
How can i translate this to an nginx location block or blocks ? I can't seem to get it right. On my last check the client browser said that something is wrong with redirecting. Here is the nginx vhost config:
Please keep in mind that there are still some tries i found, but so far i didn't have any luck. Help would be really appreciated !
thank you guys
server {
server_name example.com www.example.com;
listen 80;
root /var/www/html;
index index.php index.html index.htm;
access_log /dev/stdout;
error_log /dev/stdout info;
# FIRST .htaccess FILE
# redirect index.html to example.com/example
# the first line seems to have worked, the 2nd one is not yet tested
location /index.html { rewrite ^(.*)$ http://example.com/example/ redirect; }
location /index.html { return $301 $scheme:http://example.com/example/$request_uri; }
location / {
try_files $uri $uri/ /index.php?$args /index.html;
#try_files $uri /index.php?$args /index.html;
}
location /example {
root /var/www/html;
# try_files options:
# try_files $uri $uri/ /index.php?$args;
# try_files $uri /index.php?url=$request_uri;
# SECOND .htaccess FILE
rewrite ^/(.+)?$ /web/$1;
location /example/web {
# THIRD .htaccess FILE
# option 1
# if (!-e $request_filename){ rewrite ^(.*)$ /index.php; }
# option 2
#rewrite ^/(.*)$ /$1.php last;
try_files $uri $uri/ /index.php$args;
}
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php5:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# ocurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
check this out: Apache mod_rewrite to Nginx rewrite rules.
basically, apache rewrite and nginx rewrite do not quite mean the same thing. it seems that you are using nginx rewrite where you should be using nginx try_files.
so, you might try doing
try_files /web/$uri /web/$uri/ /web/index.php?$args
instead of
rewrite ^/(.+)?$ /web/$1;

Laravel 5.1 giving 404 Not Found except Homepage

I have a website set up on Laravel 5.1. It throws a 404 error on any page except the homepage. However, when I add index.php in the URL, it works. My site runs on a Ubuntu machine with Nginx as the web server.
Loads fine: mysite.com/index.php/dashboard
Gives 404: mysite.com/dashboard
My .htaccess in the public folder:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Thanks for your time and I would greatly appreciate any help.
Edit:
This is my Nginx conf:
server {
server_name mysite.com;
return 301 $scheme://www.mysite.com$request_uri;
}
server {
listen 80;
server_name www.mysite.com;
# note that these lines are originally from the "location /" block
root /usr/share/web/site/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/web/html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_max_temp_file_size 0;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
include fastcgi_params;
}
}
A .htaccess is for Apache. It will not work on Nginx.
Try this in your nginx site configuration (taken from the Laravel documentation.)
location / {
try_files $uri $uri/ /index.php?$query_string;
}

Rewrite all requests for non existing files to index.php with try_files with Nginx

I am trying to convert trivial htaccess file to Nginx and can't make it work. It returns 404 error.
Here is htaccess content:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Here is my current nginx config:
server {
listen 80;
server_name domain.biz;
root /var/www/domain.biz;
charset utf-8;
autoindex off;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
How about other php files you call directly? For example an info.php with just a
phpinfo();
inside?
I ask this because your server conf seems to be using try_files just right, but I'm not sure you're serving php scripts right.
¿Is your fastcgi pool listening on that sock? ¿Are you sure it isn't listening in port 9000 for example? In any case, I prefer to define an upstream in the http section and use it later in the server section
http {
upstream phpbackend {
server unix:/var/run/php5-fpm.sock;
}
...
server {
...
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass phpbackend;
fastcgi_param SCRIPT_FILENAME /var/www/domain.biz$fastcgi_script_name;
}
}
}
Are you sure your php.ini has the cgi.fix_pathinfo set to false?

Categories