Im trying to get URI Routing set up for my framework and im currently working on nginx as teh server, but the problem is i keep getting 500 error when trying to access either one of the following links
http://localhost.framework/
http://localhost.framework/index.php/
If I access the site using the following links it works:
http://localhost.framework/index.php
http://localhost.framework/index.php?/
my configuration for the domain is as follows:
server {
listen 80;
server_name localhost.framework;
root /var/www/ASFramework;
access_log /var/log/nginx/framework.access.log;
error_log /var/log/nginx/framework.error.log;
location / {
rewrite ^/(.*)$ /index.php/$1 last;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
}
basically what im trying to do is take the following url
http://localhost.framework/controller/method/../
and rewrite it to:
http://localhost.framework/index.php/controller/method/../
Within the logs (error.log) is:
2011/07/03 22:57:22 [error] 19837#0: *6 rewrite or internal redirection cycle while processing "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/", client: 127.0.0.1, server: localhost.framework, request: "GET / HTTP/1.1", host: "localhost.framework"
Can anyone tell me what's going on and how I can fix it?
Change this line:
location ~ \.php$ {
into this:
location ~ \.php.*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/ASFramework$fastcgi_script_name;
}
Your rewrite rule causes redirection cycle. nginx substitutes index.php with index.php/index.php recursively. So after second replacement your new URL will be index.php/index.php/index.php and so on.
You probably want something like this:
location / {
rewrite ^/index.php\?action=(.*)$ /$1 last;
}
Which rewrites index.php?action=someaction to /someaction.
Try this:
location / {
if ($request_uri !~ "/(index\.php)") {
rewrite ^/(.*)$ /index.php/$1 last;
}
}
Related
I'm currently trying to install a Laravel app in a sub-directory of a site using nginx to direct any traffic to that app.
I have followed the suggestions in the following Stackoverflow question, and it works perfectly Config nginx for Laravel In a subfolder
However, when using this method it removes the need for the sub-directory to be in the Laravel apps routes.
An example.
Say I am pointing all requests to https://example.com/shop to a /shop subdirectory using the following nginx entry...
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ #shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location #shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
As stated, this works but the route for the shops "homepage" in the app will actually be
Route::get('/', ShopController::class)->name('shop.home');
Whereas I need it to be
Route::get('/shop', ShopController::class)->name('shop.home');
I don't have control over all the routing etc due to it being in packages, and there are various other reasons why this would be preferable for me anyway.
Is there a way to adapt the above nginx entry to achieve this? I have tried numerous things but can not seem to get it to work.
Thanks in advance.
You need to set dynamicaly and change the fastcgi_param REQUEST_URI $request_url.
Basically to remove or add (depend on your needs) the word /shop from the REQUEST_URI
You may need to remove REQUEST_URI from fastcgi_params file.
More info and example can found in
https://serverfault.com/questions/697569/rewrite-url-with-fastcgi-in-nginx
I didn't tested it so maybe some changes need to made in some lines or change the order of things...
but you can try to use something like
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ #shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REQUEST_URI /shop$request_uri;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location #shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
I got this working thanks to a clue from #shushu304
I just updated the REQUEST_URI using the following.
location /shop {
alias /var/www/shop/public;
try_files $uri $uri/ #shop;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_URI /shop$request_uri; <--------
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
location #shop {
rewrite /shop/(.*)$ /shop/index.php last;
}
I am trying to set up a Piwigo to run on Nginx but I got into some issues with links that should contain and index.php like this:
foto.domain.net/index.php/category/3
It seems like a simple task, rewriting /index/category/3 to /index.php/category/3, but no matter what I write in those rewrites, I end up with a 404 error and a never changing error log entry that looks like this:
open() "/srv/http/foto/index/category/3" failed (2: No such file or directory), client: 94.242.246.23, server: ~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$, request: "GET /index/category/3 HTTP/1.1", host: "foto.domain.net", referrer: "http://foto.domain.net/"
The error log looks the same even if I add junk to the rewrite line such as:
rewrite ^/index((/|$).*)$ /JUNK_index.php$1 last;
So the rewrite part is faulty, somehow but I can't figure it out...
server {
listen 80;
server_name domain.net;
return 301 $scheme://www.domain.net$request_uri;
}
server {
listen 80;
server_name "~^(?<prefix>www)?\.?(?<subdomain>[a-z]+)?\.domain\.net$";
root /srv/http/$subdomain;
location #rewrites {
rewrite ^/picture((/|$).*)$ /picture.php$1 last;
rewrite ^/index((/|$).*)$ /index.php$1 last;
# The following is needed for batch operations which use i.php
rewrite ^/i((/|$).*)$ /i.php$1 last;
}
location /$subdomain {
index index.php index.html;
try_files $uri $uri.php $uri/ #rewrites;
}
location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
try_files $script_name = 404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
In general terms, as a checklist:
make sure the target/landing URI exists
this should have output "rewrite": nginx 2>&1 | grep -i -o rewrite
check your error and access log
I can't access PHP files placed in sub-directories and my CSS/JS files return a 404 in browser. Yes, I have seen many others experiencing a 404 issue but their fixes don't seem to be my solution. I've also swapped out my configuration with several online. Here is my configuration:
server {
# Port that the web server will listen on.
listen 80;
# Host that will serve this project.
server_name .stackoverflow.com;
# Useful logs for debug.
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
rewrite_log on;
# The location of our projects public directory.
root /usr/share/nginx/html/public;
# Point index to the Laravel front controller.
index index.php;
location / {
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
# PHP FPM configuration.
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# We don't need .ht files with nginx.
location ~ /\.ht {
deny all;
}
# Set header expirations on per-project basis
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;
}
}
Is there something wrong with my configuration?
I am not sure how many times this question has been answered before, but every answer that I look at gives a different approach to solving this problem of which none of them worked. I am migrating from Apache to Nginx and am facing some serious problems with setting it up. My /etc/nginx/sites-available/default looks like this...
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www/flo2go/;
index index.php index.html index.htm;
if ($request_filename !~ (js|css|images|robots\.txt|index\.php.*) ) {
rewrite ^/(.*)$ /index.php/$1 last;
}
# Make site accessible from http://localhost/
server_name localhost;
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
}
location ~ /index.php/
{
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # 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 fastcgi_params;
}
}
I have tried everything to make my web application work. All I keep getting is the 404:Page Not Found error. The site was working perfectly on Apache and after spending almost 3-4 hours in solving this problem I thought that it would be better to seek the advise of experts on this forum. Hope somebody can bail me out of this situation :(
Right config for your situation must look simular to this:
server {
server_name yoursitename.com;
root /usr/share/nginx/www/flo2go/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
expires 15d;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/flo2go/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
Main problem with current config is 2 .php location blocks and if which is evil.
please add following lines to your config
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
it works for me
Check the naming s of you Controller , View, Model and Database.
if you do this in your controller:
$this->load->view('main');
then the filename of your view must be same as you wrote in your controller:
main.php
nginx is more picky about case of file names than apache.
my controller was called Proto and in a file called proto.php. I renamed it to Proto.php and it started working.
Nginx is sensitive about case of controller file name.
My current setup is running Ubuntu 12.04 LTS, Nginx 1.4.1, with Magento 1.7.0.2.
Earlier today, I set up a basic Magento store. I haven't installed any modules and I haven't made any configuration changes. I'm just looking to get the groundwork laid before delving further into the system. I am looking to remove the "index.php" that appears between the domain name and the file path.
My vhost is taken from the nginx website:
server {
root /path/to/root/domain/html;
index index.php;
error_log /path/to/error/log/error.log;
access_log /path/to/transfer/log/transfer.log;
server_name servername.com;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location ^~ /(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
location /var/export/ { internal; }
location /. { return 404; }
location #handler { rewrite / /index.php; }
location ~* .php/ { rewrite ^(.*.php)/ $1 last; }
location ~* .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE default;
fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
}
I see that other people have have been searching for the answer to this question, there are code snippets to solutions, but none of these appear to work with my system. When I make changes to my Nginx vhost, I always restart nginx, but index.php still shows up in the URL.
Nginx config looks fine. In system > configuration > general > web in your admin panel set Use Web Server Rewrites to yes and clear the cache in magento.
Add this to magento nginx vhost.
location / {
if (!-e $request_filename) {
rewrite ^/([^?]*)(?:\?(.*))? /index.php?title=$1&$2 last;
}
if ($uri ~* "\.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$") {
expires max;
break;
}
}