Nginx server not redirecting properly - php

I'm getting a 404 error in nginx, when I should be getting a redirection instead.
I come from Apache and this is the first time I play with Nginx.
I suppose it has to be a minor mistake that I'm overlooking.
This is the code:
server {
listen 80 default_server;
server_name localhost;
root /var/www/resizing/public_html;
index index.php index.html index.htm;
location ~ ^/(.*)/cache/(.*)$
{
try_files $uri #resize;
expires 4h;
}
location / {
expires 4h;
}
location ~ \.php(.*)$ {
#fastcgi_pass 127.0.0.1:8000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location #resize {
rewrite ^/(.*)/cache/(.*)$ /resize.php?dir=$1&path=$2;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# 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:
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
For instance if I type: localhost/cache/150x200-0/originals/1.jpg I get a 404error.

The problem is that the URI is /cache/150x200-0/originals/1.jpg, and it isn't matched by the location ~ ^/(.*)/cache/(.*)$ in your configuration.
Correct location will look like (assuming the "dir" component is actually needed):
location ~ ^(/.*)?/cache/(.*)$ {
...
}
Note well that the same problem is present in the rewrite used in the location #resize.

Related

Nginx Config 2 Web Applications on one Server

Good Morning guys,
i am trying to create an working Nginx Config.
I have two web applications:
/app/web
/app/api
My URL should look like this:
10.X.X.XX => /app/web
10.X.X.XX/api => /app/api
My current config:
server {
listen 80 default_server;
index index.php index.html index.htm;
root /app/web;
location /api {
root /app/api;
}
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}
Any suggestions?
You can host multiple site if you follow below configuration. This is a working code. You can modify it according to your need
server {
# Listing port and host address
# If 443, make sure to include ssl configuration for the same.
listen 80;
listen [::]:80;
server_name 192.168.0.132;
# Default index pages
index index.php;
# Root for / shipment
root /var/www/msdsl/shipment/public;
# Handle main root / shipment
location / {
#deny all;
try_files $uri $uri/ /index.php?$args;
}
# Handle restora project, just replicate this section for further projects app3, app4
# by just replacing restora with appropriate tag(project1,/project2/project 3)
location /restora {
# Root for this project
root /var/www/msdsl/restora/public;
# Rewrite $uri=/restora/xyz back to just $uri=/xyz
rewrite ^/restora/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
location /tposreport {
# Root for this project
root /var/www/msdsl/tposreport/public;
# Rewrite $uri=/tposreport/xyz back to just $uri=/xyz
rewrite ^/tposreport/(.*)$ /$1 break;
# Try to send static file at $url or $uri/
# Else try /index.php (which will hit location ~\.php$ below)
try_files $uri $uri/ /index.php?$args;
}
# Handle all locations *.php files (which will always be just /index.php)
# via factcgi PHP-FPM unix socket
location ~ \.php$ {
# At this point, $uri is /index.php, $args=any GET ?key=value and $request_uri = /restora/xyz.
# But we don't want to pass /restora/xyz to PHP-FPM, we want just /xyz to pass to fastcgi REQUESTE_URI below.
# This allows laravel to see /restora/xyz as just /xyz in its router.
# So laravel route('/xyz') responds to /restora/xyz as you would expect.
set $newurl $request_uri;
if ($newurl ~ ^/tposreport(.*)$) {
set $newurl $1;
root /var/www/msdsl/tposreport/public;
}
if ($newurl ~ ^/restora(.*)$) {
set $newurl $1;
root /var/www/msdsl/restora/public;
}
# Pass all PHP files to fastcgi php fpm unix socket
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Use php fpm sock which is installed on your machine like php7.2, php5.6
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
# Here we are telling php fpm to use updated route that we've created to properly
# response to laravel routes.
fastcgi_param REQUEST_URI $newurl;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# Deny .ht* access
location ~ /\.ht {
deny all;
}
}`
Just copy and paste the same config into a new file
Name it the same as the subdomain you want it to run on
Give the path to the folder
Add the new subdomain to the hosts file
Restart nginx
Create 2 more servers in nginx. The first for /api (listen en 8080 for example), the other for /web (on 8081). Your main serveur (on 80/443) is then a proxy on the others :
upstream backend_api{
server 127.0.0.1:8080;
}
upstream backend_web{
server 127.0.0.1:8081;
}
server {
listen 80;
server_name www.example.com example.com;
location /api{
include proxy_params;
proxy_pass http://backend_api;
}
location / {
include proxy_params;
proxy_pass http://backend_web;
}
}
server {
listen 8080 default_server;
index index.php index.html index.htm;
root /app/api;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M; }
server {
listen 8081 default_server;
index index.php index.html index.htm;
root /app/web;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
client_max_body_size 10M;
}

nginx trying to specify different locations not the main root

I have a config that handles different "websites" a main root handles the requests for the main url and I specify a specific path for it, in another location /mailtracker I handle a different app and point it to a different directory. My problem is php-fpm is not finding this second location.
Basically I want to have several locations pointing to different roots.
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites/dorero/;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /mailtracker {
alias /var/www/sites/mailtracker/web/;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 192.168.10.3:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I tryied specifying instead of $document_root the path to the alias /var/www/sites/mailtracker/web/ and it didn't work.
Output from the php-fpm to this config says:
192.168.10.4 - 06/Feb/2018:19:04:49 +0000 "GET /mailtracker/index.php" 404
Put location /mailtracker above location / because nginx matched route /mailtracker for / before checking /mailtracker
You can remove location ~ \.php$ inside location /mailtracker
Add try_files $uri $uri/ =404; to location /mailtracker
edit:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/sites;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location #rewriteapp {
rewrite ^(.*)$ /app.php/$1 last;
}
location /mailtracker {
alias /var/www/sites/mailtracker/web;
set $subfolder "mailtracker/web";
try_files $uri #rewriteapp;
}
location / {
root /var/www/sites/dorero;
set $subfolder "dorero";
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ ^/(index|app|app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
## NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
fastcgi_pass 192.168.10.3:9000;
# # With php5-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$subfolder/$fastcgi_script_name;
}
}

Laravel: Dingo API - NGINX 404 all routes

sorry if this a double post but I can't find the problem with this I installed Dingo api on a server with NGINX, PHP5.6 but everytime I access the server routes(e.g.http://104.27.5.XXX/api/brands) I always get
Here is the routes:
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function($api) {
$api->get('/brands',function(){
$brands = Brands::all();
return $brands;
});
$api->post('/login','App\Http\Controllers\AuthenticateController#login');
$api->get('/filters', 'App\Http\Controllers\FiltersController#index');
$api->get('/dashboard', 'App\Http\Controllers\DashboardController#index');
$api->get('/dashboard/brands/{id}', 'App\Http\Controllers\DashboardController#getBrandDrillDown');
$api->get('/reports', 'App\Http\Controllers\ReportController#index');
});
Here is the .env file for the api config
API_STANDARDS_TREE=vnd
API_PREFIX=api
API_VERSION=v1
API_STRICT=false
API_DEBUG=true
Is there anything wrong???
added NGINX default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/hokair/public;
index index.php index.html;
# Make site accessible from http://localhost/
server_name http://xx.xx.xx.xx;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/html;
#}
# 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;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Your nginx configuration is misconfigured.
Follow those instruction from the lavarel setup:
https://www.digitalocean.com/community/tutorials/how-to-install-laravel-with-an-nginx-web-server-on-ubuntu-14-04

Owncloud with an Alias on Nginx

I have a Problem with our Nginx configuration, We have Wordpress in our current root directory and i would like to setup owncloud on /owncloud by using a directory outside of our root. I have tried to setup an alias in nginx but i get an "access denied" from nginx or php i'am not sure.
My nginx config:
server {
listen 134.34.60.101:80; ## listen for ipv4; this line is default and implied
# listen [::]:80 default ipv6only=on; ## listen for ipv6
listen 134.34.60.101:443 default ssl;
server_name fachschaft.inf.uni-konstanz.de www.fachschaft.inf.uni-konstanz.de;
#root /usr/share/nginx/www;
root /srv/www/website/current;
index index.php;
# reroute to old svn for now - Sammy 2013-11-26
rewrite ^/svn/fachschaft(/.*)$ https://134.34.58.21/svn/fachschaft$1;
ssl_certificate ssl/chained-nginx.crt;
ssl_certificate_key ssl/key-no-pw.pem;
ssl_session_timeout 5m;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
if ($ssl_protocol = "") {
rewrite ^ https://www.fachschaft.inf.uni-konstanz.de$request_uri? redirect;
}
#Owncloudsettings:
client_max_body_size 256M; # set max upload size
fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ /adminier {
# TODO find a better solution...
alias /srv/www/adminier/index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /owncloud {
alias /srv/www/owncloud;
try_files $uri $uri/ /index.php?$args;
# fastcgi_split_path_info ^(/owncloud/.+\.php)(/.+)$;
fastcgi_split_path_info ^/owncloud/(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
#Owncloud:
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
location ~ ^/owncloud/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}
# Roots Wordpress Theme Rewrites
# See http://roots.io/roots-101/
location ~ ^/assets/(img|js|css|fonts)/(.*)$ {
try_files $uri $uri/ /content/themes/fsinf-v2/assets/$1/$2;
}
location ~ ^/plugins/(.*)$ {
try_files $uri $uri/ /content/plugins/$1;
}
# 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;
#}
location ~ ^(.+?\.php)(/.*)?$ {
try_files $1 = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$1;
fastcgi_param PATH_INFO $2;
fastcgi_param htaccessWorking true;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
# location ~ /\.ht {
# deny all;
#}
location ~ ^/owncloud/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
}
Has anyone an idea how it will work?
1) First of all, check
fastcgi_split_path_info ^/owncloud/(.+\.php)(/.+)$;
I think You must use (.+?.php) here, ? will allow to correctly operate with *.php file as user data (I see You use it above). BTW, create info.php with
<?php
phpinfo();
?>
Upload it to You server and try download it from owncloud/remote.php/webdav/SOME_FOLDER/info.php, its must start downloading, not executing.
2) Make sure that fastcgi parameter PATH_INFO set correctly (using that info.php file), if not, try use
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
Don't ask me why it's set in such odd way, there was a bug, I don't remember where I found this solution…
3) Why You are using fastcgi_split_path_info in location /owncloud? This location is not blocking further regex matches (use location ^~ … to avoid it), so php scripts won't get there, it will be matched in location ~ ^(.+?\.php)(/.*)?$ below, which, by the way, seems doesn't have fastcgi_split_path_info.
Can't say more, sorry, I just using owncloud for my home PC
PS) I recommend You use include directive to split one huge config in multiple small configs to increase readability …

How to set up Clean url for Drupal with Nginx in localhost

I setup a nginx server in my localhost
So now I want to enable clean url for this server
(Change localhost/drupalsite/?q=abc to localhost/drupalsite/abc)
I have tried some tutorials but thet don't work for me
http://wiki.nginx.org/Drupal
https://drupal.org/node/976392
http://richardbanks.net/blog/2012/11/nginx-url-rewrites-the-correct-method
This is some basic config in nginx.conf file
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /srv/nginx/www/nginx_htdocs/;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /srv/nginx/www/nginx_htdocs/;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /srv/nginx/www/nginx_htdocs/$fastcgi_script_name;
include fastcgi_params;
}
#--------------- TUNG CUSTOM
#------------- END CUSTOM
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Here's a gist of a full configuration that works very well with Drupal's clean URL's
You must try to handle not found urls as possible query strings for index.php (if that's how Drupal works, which I'm not sure).
For example, in your first location block:
location / {
root /srv/nginx/www/nginx_htdocs/;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$request_uri;
}
This way, if
`http://mysite.com/xyz`
doesn't match any file or folder, it gets handed to index.php in the form of
`http://mysite.com/index.php?xyz`

Categories