We get information from $_SERVER['REQUEST_URI'] not from $_GET or $_POST.
I want to define $request_uri to change /example to /module/controller/action. Please note that I do not want to trigger a redirect.
I tried the code below to do this, but it doesn't work.
location /example {
rewrite /module/controller/action;
}
set $request_url $request_uri;
if ($request_uri ~ ^/example(.*)$ ) {
set $request_url /module/controller/action;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9090;
#include fastcgi.conf;
fastcgi_param REQUEST_URI $request_url;
#fastcgi_param REQUEST_URI $request_uri;
}
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 have a server running NGINX 1.10.0 and installed CodeIgniter 3 in a subdirectory /ci/. The welcome page renders when I go to the subdirectory but any other controller gives a 404 error.
Here is the code in /etc/nginx/sites-available/default configuration file:
server {
server_name mydomain.org;
root /home/username/mydomain/;
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 /home/username/mydomain/ci/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:/run/php/php7.0-fpm.sock;;
}
}
I created a Hello World Controller file (Hello.php)
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index(){
$this->load->view('helloworld');
}
}
In my config.php file, I have the following:
$config['base_url'] = '';
$config['index_page'] = 'index.php';
$config['uri_protocol'] = 'REQUEST_URI';
I have tried the approaches here:
NGINX CodeIgniter Recipe: https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/
CodeIgniter NGINX Rewrite Rules:
http://www.farinspace.com/codeigniter-nginx-rewrite-rules/
Codeigniter nginx 404 error:
Codeigniter nginx 404 error
Most of everything that I've read is based on PHP5 or an older version of CodeIgniter.
Use this config in Nginx (Source):
server {
server_name domain.tld;
root /your/web/root/path
index index.html 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;
}
}
Also you need to make a change in your php-fpm config. Edit this file:
/etc/php/7.0/fpm/pool.d/www.conf
Find this line:
listen = /run/php/php7.0-fpm.sock
Change to this:
listen = 127.0.0.1:9000
This changes should solve your issue.
Here is the final working versions of the default, www.conf and config files. Another issue I found out I was having is that the controller and file name have to be the same capitalization. The controller class Hello needs to have the filename be Hello. Some of my controllers were not the same capitalization, this however was not the sole cause of the 404 as I tried that before fixing the default file.
/etc/nginx/sites-available/default:
server {
server_name example.org;
root /home/username/example/;
index index.php index.html index.htm;
location / {
# Check if a file or directory index file exists, else route it to index.php
try_files $uri $uri/ /index.php;
# Handle PHP files
location ~* \.php$ {
root /home/username/example/ci;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri $uri/ ci/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;
}
}
/etc/php/7.0/fpm/pool.d/www.conf:
listen = 127.0.0.1:9000
Finally, in my config file:
$config['base_url'] = 'http://example.org/ci/';
This configuration changes any CI links from
http://example.org/ci/Hello
to
http://example.org/Hello
location /static {
alias /home/ubuntu/Documents/zibann/momsite/momsite/static; # your Django project's static files - amend as required
if ($uri ~* ".*config.js") {
expires off;
}
if ($uri ~* ".*\.(js|css|png|jpg|jpeg|gif|swf|svg)" ) {
access_log off;
expires 365d;
add_header Cache-Control public;
}
}
Hoping config.js would not get cached, but it does.
How can I exclude one file from being cached?
Make a separate location block for config.js above the others.
location ~ config\.js {
alias xyz;
expires off;
}
location static etc
I tried all samples above, and in other pages. The only thing worked for me was this. My file was a PHP and a I had to create a complete copy of my last part of the configuration file. Maybe replacing location ~ config.js will work for you too.
location ~ player\.php {
set $no_cache 1;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (-f $request_filename) {
# Only throw it at PHP-FPM if the file exists (prevents some PHP exploits)
fastcgi_pass unix:/tmp/run/php7-fpm.sock;
#fastcgi_read_timeout 3600;
}
}
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.
Hi I have a magento store at mysite.com
Now I want to setup a url that will run my german website at mysite.com/german
Using the same install. I already have a half working config, but my issue is that beyond the homepage all magento URLs 404. Here is my current config.
server {
listen 80;
server_name mysite.com www.mysite.com;
####
#
# BELOW THIS LINE IS MY ATTEMPT TO GET mysite.com/german to run from the same directory as mysite.com
#
####
location ~ /german(.*)\.php($|/) {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www$1.php;
fastcgi_param MAGE_RUN_CODE german;
fastcgi_param MAGE_RUN_TYPE website;
}
location ~ /german(.*) {
index index.htm index.php;
autoindex off;
alias /usr/share/nginx/www$1;
}
location ~ /deutch/ {
index index.htm index.php;
try_files $uri $uri/ #handler;
}
The old config:
###
#
# THIS BIT I THINK IS THE PART WHICH IS NOT QUITE WORKING, BUT I DON'T KNOW WHAT THE #handler PART DOES
#
###
# This redirect is added so to use Magentos
# common front handler when handling incoming URLs.
location #handler {
rewrite / /index.php;
}
####
#
# BELOW THIS LINE IS THE ORIGINAL CONFIG WHICH RUNS mysite.com NO PROBLEMS
#
####
root /usr/share/nginx/www;
location / {
index index.htm index.php;
try_files $uri $uri/ #handler;
}
# Deny access to specific directories no one
# in particular needs access to anyways.
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
# Allow only those who have a login name and password
# to view the export folder. Refer to /etc/nginx/htpassword.
location /var/export/ {
auth_basic "Restricted";
auth_basic_user_file htpasswd;
autoindex on;
}
# Deny all attempts to access hidden files
# such as .htaccess, .htpasswd, etc...
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# This redirect is added so to use Magentos
# common front handler when handling incoming URLs.
location #handler {
rewrite / /index.php;
}
# Forward paths such as /js/index.php/x.js
# to their relevant handler.
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
# Handle the exectution of .php files.
location ~ .php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
expires off;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE english;
fastcgi_param MAGE_RUN_TYPE website;
include fastcgi_params;
}
}
location ~* \.php$ {
if (!-e $request_filename) {
rewrite / /index.php last;
}
expires off;
set $runcode english;
set $runtype website;
if ( $request_uri ~* ^/german ) {
set $runcode german;
}
fastcgi_pass 127.0.0.1:9000;
#fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param MAGE_RUN_CODE $runcode;
fastcgi_param MAGE_RUN_TYPE $runtype;
include fastcgi_params;
}
That is part 1, part 2 is to ensure the code is run from the same codebase.
One solution is to symlink the directory to the root directory, exec the following in the /usr/share/nginx/www directory:
ln -s . ./german
It's filty, but it works ;)
We're using the following to redirect to the correct index file:
location /german {
if ( -e $request_filename ) {
break;
}
rewrite ^/german(.*)$ /german/index.php last;
}
#
# Redirection of subdirectory php's to their respective php files
#
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
#
# Redirect everything else to root path
#
location / {
try_files $uri $uri/ /index.php?$args;
}