Nginx rewrite equivalent of AcceptPathInfo option from Apache - mod-rewrite

I start use nginx. Before I used apache.
And default it worked well in apache (AcceptPathInfo option):
SEO friendly url:
http://testsite.com/anyfile.php/get_param1/get_value1/get_param2/get_value_2 ... etc
As the result, converted from apache file looked:
http://testsite.com/anyfile.php?get_param1=get_value1&get_param2=get_value_2 ... etc
But, how can use it nginx? And it worked for many and for any files: anyfile.php, anyfile1.php, anyfile2.php, etc... a lot of files used it.
Is it possible? Because I saw a lot of answers, but not for any file.
Many thanks.

location ~ [^/]\.php(/|$) {
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000 (or your php-fpm socket);
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
https://docs.moodle.org/29/en/Nginx#Slasharguments

Related

How To Deploy Laravel Application on Nginx Ubuntu 16.04 Server?

I want to deploy simple laravel app on nginx ubuntu 16.04 server .
I have put my app inside /var/www/html folder
on my server , i have already setup php and mysql .
i have added below code in my /etc/nginx/sites-enabled/myapp.com
server {
listen 80;
listen [::]:80;
root /var/www/html/MyApp/public;
index index.php index.html index.htm index.nginx-debian.html;
charset utf-8;
server_name myapp.com;
error_log /var/log/nginx/myapp.com.log error;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
but instead of running app , it is downloading file , so i have searched and found stackoverflow link :
Nginx serves .php files as downloads, instead of executing them
But this in Not working in my case .
I have not much idea about nginx , please help me .
nginx will show error 502 (bad gateway) when php-fpm is not running or it can't find the socket.
In your case, I think the problem is on this line:
fastcgi_pass unix:/run/php/php7.0.22-fpm.sock;
try changing it to
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
save and reload or restart nginx.

Nginx vhost cache symlink

I've got a problem with deploying my application. I have a PHP application and I deploy my application with Capistrano to my server.
Capistrano makes a new release folder with the latest version of my application and my current folder symlinks to the that release. That works fine, it really links the latest release.
But when I go the the URL of my website nothing changes, the files are from the old release folder even when the symlink links to the current folder (latest release).
Does Nginx cache all my files? Or does it cache my symlinks, I have no idea.
Folder structure:
current (symlink new release)
releases
new release
old release
Vhost:
server {
listen 443;
server_name servname.com;
root /apps/application/production/current/public;
}
The problem is at real path cache level. It caches the PHP file with the symlink path. What you need to do is provide the real document path.
You need to add these 2 lines in your config file
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
The important part is $realpath_root.
From the documentation:
$realpath_root
an absolute pathname corresponding to the root or alias directive’s value for the current request, with all symbolic links resolved to real paths
Meaning $realpath_root solves all symlinks to their real path. This is the important part.
So your location ~ \.php$ would become
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
Make sure the include fastcgi_params if present does not overwrite the 2 directives you just added.

Nginx + PHP-FPM is very slow on Mountain Lion

I have set up Nginx with PHP-FPM on my MacBook running ML. It works fine but it takes between 5 and 10 seconds to connect when I run a page in my browser. Even the following PHP script:
<?php
die();
takes about 5 seconds to connect. I am using Chrome and I get the "Sending request" message in the status bar for around 7 seconds. If I refresh again it seems to work instantly, but if I leave it for around 10 seconds it will "sleep" again. It is as if nginx or PHP is going to sleep and then taking ages to wake up again.
Edit: This is also affecting static files on the server so it would seem to be an issue with DNS or nginx.
Can anyone help me figure out what is causing this?
nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/plain;
server_tokens off;
sendfile on;
tcp_nopush on;
keepalive_timeout 1;
gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_types text/plain text/css text/javascript application/json application/x-javascript text/xml application/xml application/xml+rss;
index index.html index.php;
upstream www-upstream-pool{
server unix:/tmp/php-fpm.sock;
}
include sites-enabled/*;
}
php-fpm.conf
[global]
pid = /usr/local/etc/php/var/run/php-fpm.pid
; run in background or in foreground?
; set daemonize = no for debugging
daemonize = yes
include=/usr/local/etc/php/5.4/pool.d/*.conf
pool.conf
[www]
user=matt
group=staff
pm = dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500
listen = /tmp/php-fpm.sock
;listen = 127.0.0.1:9000
php_flag[display_errors] = off
sites-available/cft
server {
listen 80;
server_name cft.local;
root /Users/matt/Sites/cft/www;
access_log /Users/matt/Sites/cft/log/access.log;
error_log /Users/matt/Sites/cft/log/error.log;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
include fastcgi_php_default.conf;
}
fastcgi_php_default.conf
fastcgi_intercept_errors on;
location ~ .php$
{
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
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_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_read_timeout 300;
fastcgi_pass www-upstream-pool;
fastcgi_index index.php;
}
fastcgi_param REDIRECT_STATUS 200;
One reason could be - as already suspected above - that your server works perfectly but there is something wrong with DNS lookups.
Such long times usually are caused by try + timeout, then retry other way, works, cache.
Caching of the working request would explain why your second http request is fast.
I am almost sure, that this is caused by a DNS lookup, which tries to query an unreachable service, gives up after a timeout period, then tries a working service and caches the result for a couple of minutes.
Apple has recently made a significant change in how the OS handles requests for ".local" name resolution that can adversely affect Active Directory authentication and DFS resolution.
When processing a ".local" request, the Mac OS now sends a Multicast DNS (mDNS) or broadcast, then waits for that request to timeout before correctly sending the information to the DNS server. The delay caused by this results in an authentication failure in most cases.
http://www.thursby.com/local-domain-login-10.7.html
They are offering to set the timeout to the smallest possible value, which apparently is still 1 second - not really satisfying.
I suggest to use localhost or 127.0.0.1 or try http://test.dev as a local domain
/etc/hosts
127.0.0.1 localhost test.dev
EDIT
In OSX .local really seems to be a reserved tld for LAN devices. Using another domain like suggested above will def. solve this problem
http://support.apple.com/kb/HT3473
EDIT 2
Found this great article which exactly describes your problem and how to solve it
http://www.dmitry-dulepov.com/2012/07/os-x-lion-and-local-dns-issues.html?m=1
I can't see anything in your configuration that would cause this behaviour alone. Since the configuration of Nginx looks OK, and this affects both static and CGI request, I would suspect it is a system issue.
An issue that might be worth investigating is whether the problem is being caused by IPv6 negotiation on your server.
If you are using loopback (127.0.0.1) as your listen address, have a look in /etc/hosts and ensure that the following lines are present:
::1 localhost6.localdomain6 localhost6
::1 site.local cft.local
If this doesn't resolve the issue, I'm afraid you'll need to look at more advanced diagnostics, perhaps using strace or similar on the Nginx instance.
(this started as a comment but it's getting a bit long)
There's something very broken here - but I don't see anything obvious in your config to explain it. I'd start by looking at top and netstat while the request is in progress, and checking your logs (webserver and system) after the request has been processed. If that still reveals nothing, then next stop would be to capture all the network traffic - most likely causes for such a long delay are failed ident / DNS lookups.
Barring any configuration-related issues, it may be a compile issue.
I would advise that you install nginx from homebrew, the OS X open source package manager.
If you don't yet have homebrew (and every developer should!), you can grab it from here or just run this in a terminal:
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Then run
brew install ngnix
And ngnix will be deployed from the homebrew repository. Obviously, you'll want to make sure that you removed your old copy of nginx first.
The reason I recommend this is that homebrew has a number of OS X-specific patches for each open source library that needs it and is heavily used and tested by the community. I've used nginx from homebrew in the past on OS X and had no problems to speak of.

nginx, fastcgi and how to rewrite a URL

I have a very simple rewrite in nginx x using fastcgi.
Suppose my domain is www.test.com/test.fcgi
How do I rewrite to www.test.com? If I go to www.test.com/test.fcgi it works.
server {
listen 80;
server_name 127.0.0.1;
location ~ \.fcgi$ {
rewrite ^/test.fcgi/(.*)$ $1 last;
root /var/www;
include /etc/nginx/fastcgi_params;
#fastcgi_pass unix:/tmp/nginx.socket;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;
include fastcgi_params;
}
}
Well, you really wouldn't use a .fcgi file like that with Nginx, so it's hard to answer the question as asked. Nginx works with FasCGI the same way it works with other upstream servers, by passing the request to a Unix or TCP socket.
You would rewrite the request using one of the standard methods for rewriting a request.In your location section use something like this:
rewrite ^/test.fcgi/(.*)$ $1 last;
Then, you pass the request to a daemon listening for FastFGI requests, like this:
fastcgi_pass localhost:8001;
fastcgi_index index.fcgi;
You might need other options depending on the backend processand the specifics of your setup, if you provide us with moreinformation, we may be able to help further.
For documentation and examples for these two modules see (http://wiki.nginx.org/HttpFcgiModule)[here] and (
Add this to your nginx config,guess it will help you.
location = / {
root /var/www/
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;
include fastcgi_params;
}

How do I configure nginx and CodeIgniter?

I'm running nginx on my home computer for development. I also have it linked to DynDNS so I can show progress to my co-worker a bit easier. I can't seem to get nginx to rewrite to CodeIgniter properly. I have CodeIgniters uri_protocol set to REQUEST_URI.
All pages that should be showing up wtih content show up completely blank. If I phpinfo(); die(); in the index.php file of Codeigniter, it works as expected and I get the phpinfo.
Also, pages that should give a 404 give a proper CodeIgniter 404 error.
Here's what I have so far.
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)";
root /home/zack/Development/public_html;
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name zackhovatter.dyndns.info;
index index.php;
root /home/zack/Development/public_html;
location /codeigniter/ {
if (!-e $request_filename) {
rewrite ^/codeigniter/(.*)$ /codeigniter/index.php/$1 last;
}
}
#this is for the index.php in the root folder
location /index.php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/zack/Development/public_html/index.php;
include fastcgi_params;
}
location /codeigniter/index.php {
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/zack/Development/public_html/codeigniter/index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param PATH_INFO $document_uri;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass 127.0.0.1:9000;
}
}
}
Actually I got this working. Not sure how, but I think it was file permissions being set incorrectly or something.
Edit:
For those who are wondering, I had an older version of PHP installed it seems. Reverting back to old class constructors fixed the issue. Darnit.
I have exactly the same problem (Codeigniter gives blank pages and the error pages and php are working) and i found that the problem was from the mysql db driver which should be changed to mysqli driver .
So just we need to change the db driver in config/database.php
$db['default']['dbdriver'] = 'mysqli';
And this is due that the mysql driver is deprecated in the new versions of php.
CodeIgniter4 How To Setup NGINX Server Blocks (Virtual Hosts) on RasperyPi Debian 10.4
I am using a RasperyPi-4 as web developing server with different projects running on it.
Visual Studio Code enables editing files via Remote-SSH extension very easily.
Setting up CI4 on NGINX gives you the opportunity to run different projects on the same server.
Because it took me some days to get this configuration running I will give you a quick reference guide to setup quick and easy.
If you have not installed NGINX and composer yet please have a look here:
http://nginx.org/en/docs/beginners_guide.html
https://codeigniter4.github.io/userguide/installation/installing_composer.html
https://getcomposer.org/download/
CodeIgniter4 installation via composer
server ip: 10.0.0.10
port: 8085
project name is 'test'
Please modify to your needs!
cd /var/www/html
composer create-project codeigniter4/appstarter test
the command above will create a 'test' folder /var/www/html/test
Modify the 'env' file
sudo nano /var/www/html/test/env
app.baseURL = 'http://10.0.0.10:8085'
Important: Do NOT use 'localhost' for the server URL!!!!!
please uncomment:
# CI_ENVIRONMENT = production
and modify:
CI_ENVIRONMENT = development
Save the file as '.env' to hide the file
NGINX Server Blocks
cd /etc/nginx/sites-available
touch test
sudo nano test
Paste this into the file and save after modifying to your requirements:
server {
listen 8085;
listen [::]:8085;
server_name test;
root /var/www/html/test/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php-fpm:
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# With php-cgi:
# fastcgi_pass 127.0.0.1:9000;
}
error_page 404 /index.php;
# deny access to hidden files such as .htaccess
location ~ /\. {
deny all;
}
}
After saving the file create a symbolic link to sites-enabled:
cd /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/test
Modify File and Group Permissions
chown -v -R pi:www-data /var/www/html/test
sudo chmod -v -R 775 /var/www/html/test
Start NGINX
sudo service nginx start
Open CI4 Welcome Page
http://10.0.0.10:8085
Here is a solution.
https://web.archive.org/web/20120504010725/http://hiteshjoshi.com/linux/secure-nginx-and-codeigniter-configuration.html
Also, I had to change $config['uri_protocol'] = “DOCUMENT_URI”; to make it work.
UPDATE: Fixed the 404 url from web archive.

Resources