Laravel4-nginx-fpm file not found on rewrite - php

Hello I am using laravel 4 on Nginx with FastCGI, I am pretty new to Nginx and FastCGI.
The situation is when I use the urls as $doc_root/index.php?my_uri it works but when I try pretty URLs it does not work.
this is my nginx configuration
server {
listen 80;
server_name localhost;
rewrite_log on;
index index.php;
root /home/mostafa/public_html;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
#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;
}
# 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$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /home/mostafa/public_html$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
expires 365d;
}
}
and this is my FastCGI file
;prefix = /path/to/pools/$pool
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses on a
; specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = /var/run/php5-fpm.sock
The case is when I try a url like this
http://localhost/laravel-master/public/index.php?current it works and when I try it with out the index.php it does not work, and gives a file not found and in error log it says
FastCGI sent in stderr: "Primary script unknown" while reading
although according to this tutorial it should work
http://daylerees.com/nginx-configuration-for-daylerees-dot-com

Ok after searching for a few while and so many try outs, I found that the error was in my nginx config document root, it must be set to laravel-master/public/
The error I was getting was because the rewritten URLs were forwarded to $document_root/index.php which is localhost/index.php which of course does not exist, so changing the document root to my laravel master did the trick and the URLs where rewritten to localhost/laravel-master/public/index.php

Related

Configuring adminer with NGINX: "no port in upstream php" error

I have set up MeteorJs application with MongoDB backend in Digital Ocean. I am now trying to set up adminer so I can query MongoDB without opening input ports on my droplet. Everytime I try to reload the nginx settings, I get nginx: [emerg] no port in upstream "php" in /etc/nginx/sites-enabled/admin:32 error.
What am I missing?
/etc/nginx/sites-enabled/admin
server {
listen 80;
server_name 165.227.197.220;
return 301 https://$server_name$request_uri;
}
server {
server_name 165.227.197.220;
listen 443 ssl;
access_log /var/log/nginx/admin.access.log;
error_log /var/log/nginx/admin.error.log;
ssl_certificate /etc/nginx/ssl/admin.crt;
ssl_certificate_key /etc/nginx/ssl/admin.key;
root /var/www/admin;
index adminer.php;
# Get file here https://codex.wordpress.org/Nginx
include global/restrictions.conf;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd/admin;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php;
fastcgi_index adminer.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
/etc/nginx/global/restrictions.conf
# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
You need to setup an upstream for example using wordpress that is what we have
upstream index_php_upstream {
server 127.0.0.1:8090; # NGINX Unit backend address for index.php with
# 'script' parameter
}
For more about upstream, so can view this article UpStream - Nginx

Nginx php-fpm 404 error for one request and not another

I am hosting multiple php slim applications on the same server. They are located at the path apis/'tier'/'organization'/'appName'/'version' so for example apis/FreeTierSmall/master/exampleApp/v1.
I am using Nginx with php-fpm and am getting a very weird bug. I am trying to redirect any requests that start with apis/master/ to apis/FreeTierSmall/master. I've turned on the Nginx rewrite_log and can see that the files get redirected correctly. I get the correct result if I try apis/FreeTierSmall/master/example/v1. However, I get a 404 error if I try apis/master/example/v1 which redirects to the same php file. I know the redirect is working because I can see it in the logs. It seems that there is some problem with php-fpm. I am adding a header to the php-fpm execution so I know that it is calling the correct script. For some reason though a request of the same file is producing a 404 error in one case and not the other.
Is there some parameter that could cause the same file passed to fpm to work in one instance and not the other?
Here is my nginx configuration:
worker_processes 1;
pid /run/nginx.pid;
user nginx www-data;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main_timed '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'$request_time $upstream_response_time $pipe $upstream_cache_status'
'FPM - $document_root - $fastcgi_script_name > $request';
access_log /var/log/nginx/access.log main_timed;
# error_log /dev/stderr notice;
error_log /var/log/nginx/error.log debug;
# error_log above can be debug
rewrite_log on;
keepalive_timeout 65;
server {
listen [::]:80 default_server;
listen 80 default_server;
server_name _;
sendfile off;
root /var/www/html;
index index.php index.html;
error_page 404 /404.html;
# NOTE: Once you use last, that is the last redirect you can do. You must find the file after that.
# HEALTH CHECK
location /apis/FreeTierSmall/elb-status {
access_log off;
return 200 'A-OK!';
# because default content-type is application/octet-stream,
# browser will offer to "save the file"...
# the next line allows you to see it in the browser so you can test
add_header Content-Type text/plain;
}
# NORMAL API PATHS
location /apis/ {
#rewrite the old apis
rewrite ^/apis/master/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/master/$1/$2/api.php$3 last;
rewrite ^/apis/interfaceop/([\w-]+)/([\w-]+)(.*)$ /apis/FreeTierSmall/interfaceop/$1/$2/api.php$3 last;
# add api.php to the path of the file
rewrite ^/apis/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)(.*)$ /apis/$1/$2/$3/$4/api.php$5 last;
}
# ANY OTHER FILES
location / {
# try to serve the file, the directory, or a 404 error
add_header X-debug-message-2 "A static file was served or 404 error $uri" always;
try_files $uri $uri/ /robots.txt; # Need to change back to =404
}
# ERRORS
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/lib/nginx/html;
}
# PHP FILES
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php {
add_header X-debug-message-5 "fastCGI -> .php $document_root$fastcgi_script_name" always;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# SECURITY CONCERNS
# deny access to . files, for security
location ~ /\. {
log_not_found off;
deny all;
}
}
}
Turns out, the issue is that the URI of the request doesn't get changed with rewrite. Slim was providing the 404 error because the route didn't exist, and the route didn't exist because the URI never got changed by rewrite. So, rewrite doesn't actually change the request parameters it's just used to decide what file is going to be served. This is ok for most use cases but horrible for apis... a very odd error to debug. Good luck whoever runs into this in the future.
The solution: use proxy_pass.
location /apis/master/ {
# Reroutes /apis/master/* to /apis/FreeTierSmall/master/* correctly!
proxy_pass http://localhost:80/apis/FreeTierSmall/master/;
}

Base URLs like example.com not working in NGinx

I have 12 sites that I plan to run on a single server that has NGinx and php5-fpm on it. I set them all up using one server block per conf file, all included by the main nginx.conf file. It's a mix of Wordpress, PhpMyAdmin, and PHP sites. The wordpress and PhpMyAdmin sites are working fine, but the PHP sites are not. Meaning, when I pull up example.com, Chrome says connection refused, and there's no trace of an incoming connection on NGinx logs. test.example.com pulls up the default site(because I didn't configure test.example.com then) at the same time.
I copied the nginx configs from the working sites to set up the sites that are not working, but no luck. The only difference in nginx config between the working and non-working sites are the server_name directive.
After checking and rechecking for over 2 hours, I found out that the sites that have the server_name as pqr.example.com work, but the ones with example.com don't. All of the working sites are configured to use subdomain URLs, and that's probably why they're working.
My questions are -
1. What am I missing in the config to make the abc.com work ?
2. I have two sites, example.com and example.net that I'm trying to run on the same server. Is that going to be a problem for NGinx ?
3. Does Nginx have a problem with differentiating between example.com, test.example.com, and example.net ?
4. I also noticed that if www.example.net works, www.example.com doesn't and vice versa, which means I have to assign each site that has the name abc in it different subdomains like www.example.net and test.example.com. Is this a standard/expected behavior of Nginx, or am I missing something ?
5. All of my base URLs auto redirect from http://example.com to http://www.example.com; How do I find out where that redirect is happening ?
Below are the Nginx config files that I'm having problems with, truncated to include the important parts; Please let me know if more info is needed.
Main nginx.conf file -
user www-data www-data;
pid /var/run/nginx.pid;
worker_processes 4;
worker_rlimit_nofile 100000;
events {
worker_connections 4096;
include /etc/nginx.custom.events.d/*.conf;
}
http {
default_type application/octet-stream;
access_log off;
error_log /var/log/nginx/error.log crit;
.......
server_tokens off;
include proxy.conf;
include fcgi.conf;
include conf.d/*.conf;
include /etc/nginx.custom.d/*.conf;
}
include /etc/nginx.custom.global.d/*.conf;
Here is the full working .conf file for a blog that works. All other sites have this full config, since they are just basic PHP sites.
server {
listen *:80;
server_name blog.example.com;
access_log /var/log/nginx/blog-example.access.log;
error_log /var/log/nginx/blog-example.error.log;
root /var/www/example/blog;
index index.html index.htm index.php;
# This order might seem weird - this is attempted to match last if rules below fail.
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~ /\. {
deny all;
}
# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location ~ [^/]\.php(/|$) {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won't get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fcgi.conf;
fastcgi_pass unix:/var/run/php-fcgi-blog-example-php-fcgi-0.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here's the truncated .conf file for example.com
server {
listen *:80;
server_name example.com www.example.com test.example.com;
access_log /var/log/nginx/examplecom.access.log;
error_log /var/log/nginx/examplecom.error.log;
root /var/www/example/com;
index index.html index.htm index.php;
# This order might seem weird - this is attempted to match last if rules below fail.
location / {
try_files $uri $uri/ /index.php?$args;
}
........
location ~ [^/]\.php(/|$) {
......
fastcgi_index index.php;
include fcgi.conf;
fastcgi_pass unix:/var/run/php-fcgi-examplecom-php-fcgi-0.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here's the truncated file for example.net
server {
listen *:80;
server_name example.net www.example.net test.example.net;
access_log /var/log/nginx/examplenet.access.log;
error_log /var/log/nginx/examplenet.error.log;
root /var/www/example/net;
index index.html index.htm index.php;
# This order might seem weird - this is attempted to match last if rules below fail.
location / {
try_files $uri $uri/ /index.php?$args;
}
........
location ~ [^/]\.php(/|$) {
......
fastcgi_index index.php;
include fcgi.conf;
fastcgi_pass unix:/var/run/php-fcgi-examplenet-php-fcgi-0.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Meaning, when I pull up example.com, Chrome says connection refused, and there's no trace of an incoming connection on NGinx logs. test.example.com pulls up the default site(because I didn't configure test.example.com then) at the same time.
Well, your server is listening. Chances are you haven't configured your DNS records correctly, or there is DNS caching. Set your host file to test this theory.

Wordpress running on nginx on a subdomain doesn't work on port 80

I have a server hosting two Django websites and a wordpress blog, all on nginx. I have setup one Django site to be served at the top level domain, and the other at a subdomain.
I have not been able to get the Wordpress site to work when it is on port 80. If I change the port to 8079, it works correctly.
Here is my nginx conf for the Wordpress site:
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name sub2.mydomain.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
#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;
}
}
One thing I have noticed is that even after I change the port back to 80 and restart nginx, when I go to the url in my web browser, it will redirect to port 8079. I'm not sure if that is caused by the browser cache, or DNS or something on my server.
Here are my other nginx configs:
upstream mydomain_app_server {
server unix:/webapps/mydomain/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.mydomain.com;
client_max_body_size 4G;
access_log /webapps/mydomain/logs/nginx-access.log;
error_log /webapps/mydomain/logs/nginx-error.log;
location /static/ {
alias /webapps/mydomain/static/;
}
location /media/ {
alias /webapps/mydomain/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://mydomain_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/mydomain/static/;
}
}
and my last one is identical except with different paths and the following server_name:
server_name sub2.mydomain.com;

Nginx Wave Framework API configuration issue - WARNING: Nginx HttpRewriteModule is not supported

I have searched for a few days and tried various configurations through trial and error but I have not been able to correct my configuration. My specialty is in database design and development so server configuration has been challenging.
I am on a LEMP stack and I installed the Wave Framework. Wave is a PHP micro-framework that is built loosely following model-view-control architecture and factory method design pattern
http://www.waveframework.com/wave/doc/index.htm
Surprisingly it's pretty easy to get on your server however I can't resolve one issue. On my server I added in the lines into my nginx config file the Wave suggested and I still get the warning "WARNING: Nginx HttpRewriteModule is not supported, Index Gateway and rewrite functionality will not work, this warning can be ignored if Index Gateway is not used"
Besides this I have been able to use a lot of the features of the Wave Framework and got a portion of my Model and Controller coded.
Please help, my config is pasted below.
nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
rewrite_log on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-enabled/default
(i changed my domain name)
server {
listen 80;
root /usr/share/nginx/www;
index index.php;
server_name *.example.com;
# This is for making sure that files in /resources/static/ folder don't get parsed with PHP
location ^~ /resources/static/ {
break;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# Rewrite that directs everything, except PHP to index file
# Make sure you place this before your "location ~ .php$ {" for the server configuration.
location / {
rewrite ^(.*)$ ./index.php last;
}
# Pass php scripts to the php engine
location ~ \.php$ {
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;
}
}
http://www.example.com/tools/compatibility.php
(output for my domain - wave framework)
SUCCESS: PHP is version 5.3.0 or above (running 5.5.9-1ubuntu4.3)
SUCCESS: PHP setting short_open_tag is enabled
SUCCESS: PDO is supported
SUCCESS: PDO MySQL is supported
...
WARNING: Mcrypt PHP extension is not supported, this is optional and used only when API requests are made with www-crypt-input and www-crypt-output requests
SUCCESS: Zip is supported
SUCCESS: FTP is supported
WARNING: Memcache is not supported, this can be ignored if you do not intend to support Memcache as a caching layer
SUCCESS: GD Graphics Library is supported
SUCCESS: Nginx server is used
WARNING: Nginx HttpRewriteModule is not supported, Index Gateway and rewrite functionality will not work, this warning can be ignored if Index Gateway is not used
SUCCESS: /filesystem/ is writable
SUCCESS: /filesystem/cache/ is writable
...
SUCCESS: /filesystem/data/ is writable
I am not worried about the other warnings beyond the HttpRewriteModule
Thank you in advance!
So I (with help) answered my own question. Anyone who tries Wave Framework with Nginx will encounter this (at least with 3.7.1 current release). Wave Framework has a bug with the Nginx HTTPrewriteModule and the config file the provide for you to implement into your Nginx config is wrong. Below is my etc/nginx/sites-enabled/default config file that works for me.
# this file is /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /var/log/nginx/default/access.log;
error_log /var/log/nginx/default/error.log;
root /var/www/default;
index index.php index.html index.htm;
# domain names of this vhost (important if there are more
# than one vhost on the same IP:PORT combination)
server_name localhost mydomain.com alias.mydomain.com;
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
# deny access to hidden files, that is the ones which names that start
# with . (dot)
location ~ /\. {
deny all;
}
# This is for making sure that files in /resources/static/ folder don't get parsed with PHP
location ^~ /resources/static/ {
break;
}
# Uncomment to satisfy compatibility check for Nginx rewrite module
# (based on .htaccess delivered with Wave Framework)
# location ~ compatibility\.php$ {
# if ($query_string != rewrite_enabled) {
# rewrite ^(.*)$ $1?rewrite_enabled break;
# }
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
# }
location / {
rewrite ^(.*)$ /index.php last;
}
# Uncomment if you have a custom 404 page
#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
location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

Categories