Lumen application work with trailing slash using Nginx server - php

all I am new to Lumen cause I was using something else before. I have tested Lumen project and find out that it works well with nice support and documentation and can be extend to Laravel which is a bigger project. So I decide to use Lumen for a company,
The only problem I am currently facing now is trailing slash of url on Nginx.
e.g.
$app->get('welcome', function() { return 'Hello'; });
it responds to
http://mysite.dev/welcome
but with a trailing slash
http://mysite.dev/welcome/
the site throws a 404.
This is because the old website been using all url with end slash for e.g. PPC, SEO ... more, They do not want to redo and change the whole process including 3rd party who use these url and they can not do 301 from url with ending / to redirect to url without ending / which will cause too much redirection.
I have tried searching for solution for whole week now but still can not find any solution that best match this user requirement.
Is there any way for lumen to revert routing url to work with ending / and not working with without ending / ??
or otherwise could you please recommend me to use something else? To match this requirement.
I also tried this service provider still not working link
Regards

Lumen and Laravel should be fine with trailing slashes.
The 404 is likely resulting from a bad nginx configuration.
This is how my nginx config looks like to catch the trailing slash and use the same content at that location.
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
}

If you're using Nginx, try this config:
index index.html index.htm index.php;
location #rewrite {
rewrite ^/(.*)$ /index.php;
}
location / {
try_files $uri $uri/ #rewrite;
}
I once encountered a similar problem,after debugging with RoutesRequest.php, I discovered that the 5.4 version no longer needs the $query_string or $args things(have no reason why most tutorials still have them).
Hope this helps.If not, maybe you could try debugging with RoutesRequest.php too, adding some helpful outputs will certainly help you locate the problem.

Related

Converting Rewrites from Apache to NGINX

I have a custom PHP application which follows the basic MVC pattern of development. The applications directory structure is as follows:
admin/
default/
index.php
In the admin is another index.php (which is how it handles requests to /admin).
In apache, this worked by putting a .htaccess file in each directory (admin and doc root) setting up a rewite and the application works. In NGINX, it doesnt seem so simple.
I can get the basic "default" application to work, by using this in my nginx.conf:
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?path=$1 last;
}
(we run on vhosts so I cannot reference location / or I get a duplicate rule error).
This gets the frontend application to work, but when I try to access the /admin portion of the application the login screen loads, but when I try to submit and it tries to hit the endpoint 'admin/index/index' it fails, as my rewrite rule doesnt work. Here is what I have for the rewrite in NGINX:
location /admin {
try_files $uri $uri/ /admin/index.php?path=$uri&$args;
}
I think the issue is that the $uri being passed in is /admin/index/index instead of it being what it should be and is under Apache: /index/index.
Can anyone help me correct these NGINX rules so that my application works properly?
Thanks in advance.
As you said, you pass in the extra $uri. Try this:
location /admin {
try_files $uri $uri/ /admin/index.php?$args;
}

Yii1 wrong route when URL contains trailing slash

I have to support one old project which uses Yii1. I get very strange behavior. Pretty URL is configured.
When URL doen't have a trailing slash (e.g. /about or /blog/post/5) everything goes OK (the route is correct and the correct page is displayed). But the same URLs with trailing slash (/about/ or /blog/post/5/) make a wrong route. Disregarding of url I always get the route 'main/index' and index page is displayed (not redirected to, but displayed at all URLs with trailing slash).
Any ideas?
I had the same issue and the problem was in nginx config. Yii's pretty URLs require proper config to work. This was strange but the same config worked fine on one server and produced the described behavior on another server.
The problematic config:
location ~ ^(.+\.(js|css|jpeg|jpg|gif|png|ico|swf|mp3|html|eot|woff|ttf|otf|svg|zip|pdf|xml))$
{
rewrite ^(.*)/$ $1 permanent;
try_files $uri /index.php?$args;
}
The working config:
location /
{
index index.php;
if (!-e $request_filename)
{
rewrite ^/(.*) /index.php?r=$1 last;
}
}

.html causes 404 error

I am running a joomla 2.5 site on ubuntu server with php/nginx/mysql (fairly new to nginx)
My problem is that when a user hits a url, I need it to ignore the .html file extension.
For example, if you hit mysite.com/page then it renders the page fine.
If you hit mysite.com/page.html then it will throw a 404 error. Which is because there isn't actually a 'page.html' page on my site. Its a K2 article alias. Yes, I could not put .html in but its not me adding content to the site, its the client. I have recently moved server and before it worked fine, now it doesnt so I know I have missed something in the config.
I know I can get nginx to do the opposite of what I want, with try files. Not sure how to get it to do the reverse.
This is my nginx config:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
Now I know if i had a page.html page and I wanted to hide the .html I could add $uri.html
What I want to know is how to get nginx to try page.html if it cant find it try page
So turned out to be a Joomla Config variable. Under SEO settings in global config set 'apply suffix' to yes. I feel quite stupid. Thanks to all those that tried to help, as always much appreciated.
Here's a potential solution I managed to come across:
location / {
# If /{foo} is not an existing file or directory, rewrite to /{foo}.html
if (!-e $request_filename) {
rewrite ^(.+)$ /$1.html break;
}
# Redirect all {foo}.html URLS to /{foo}
rewrite ^/(.*)\.html$ /$1 redirect;
}
If you can correctly write .htaccess rewrite lines, try using a htaccess-to-nginx converter
As commented, this will check for the existence of a directory, file, etc. using the current URL. If nothing exists, it will attempt to request the url with .html appended.
Additionally, if .html is already present in the URL, it will redirect to the URL without .html
Quick workaround:
rewrite ^(. *)\.html$ $1 break;
Should be the first rewrite in the stack, even before try_files.

Codeigniter on Nginx, url segments

I've been developing a website with Codeigniter on my local machine, running an Apache server. I did not realise the production server would be running Nginx. When attempting to run CI, I now run into the problem that those pretty URLs with segments in them do not work. I keep getting a 404 page.
I have no experience with Nginx, but I found a few code snippits via Google that I tried.
I'm in a shared hosting situation, meaning I have limited configuration options. This results in the configuration interface rejecting most of the configuration snippits I've copy-pasted into it in an attempt to get it to work.
So far, I've found out it rejects the keywords server_name, root and include, which seem to appear in every single solution I've found.
As I have little knowledge on the subject, I'm not sure if what I'm trying to do (i.e. get Codeigniter up and running with slash-separated URL parts rather than a query string) is even possible when I'm not able to use the afformentioned keywords.
Is there a 'default' piece of Nginx configuration available for Codeigniter that might help me out here? Or is my situation too limited to even allow for a solution? Should I just ask my host for help?
EDIT: Note that I'm not trying to remove index.php from my URLs to make them more appealing - I'm not at that point yet. This is about URL segments in general - you know, the default behaviour of Codeigniter.
Then you say "Slash-separated URLs", you must understand, that it's just some URL, that is leads to non-existing file (for example, site.com/controller/action/param1/value1 leads to "folders" /controller/action/param1 and "file" value1), and tghis situation is solved by using mod_rewrite in apache - it just rewrites any URL, that points to non-existing file to a url, pointing to index.php. So, In nginx you need just the same.
In your nginx configuration you have to add this locations:
# for rewriting non-existing url-s to index.php
location / {
try_files $uri $uri/ /index.php?$args;
}
and
#location, that enables to process *.php files thorugh php-fpm (fastcgi)
#it's possible that you already have this block configured, and don't need to change it.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9999;
fastcgi_index index.php; include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_ignore_client_abort off;
}
Also, If it doesn't help, just google "Configure nginx for codeigniter", or, ask your webhoster support "please, make changes to config so url rewriting could start to work"

PHP front controller in nginx

I have a wiki that hosts user-generated content with URLs like /wiki/view/pagename and /wiki/modify/pagename. I'm using an nginx configuration that goes something like:
location /wiki/ {
try_files $uri $uri/ /wiki/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi stuff...
}
It's been working great and as far as i can tell, this is the recommended approach. However, today, a user created a page named "whatever.php", so it needs the URLs /wiki/view/whatever.php to be redirected to my /wiki/index.php... but it gets caught in the second location block and returns a 404 to the user-agent.
Does anyone have any suggestions? Can i add an extra location block to rewrite *.php to the main script somewhere in such a way that won't affect actually routing pages? I still want to use nginx to serve static content inside the /wiki/ directory and to preserve the behaviour of everything outside this directory.
Repost of this dead forum thread

Categories