Nginx - Redirect 404 PHP files - php

I'm using Laravel's valet on my local. I did not have a problem with this on my local. My remote server uses Ubuntu 16.04.
I have an index.php like so in my website's root:
<?php
require __dir__ . '/src/core/bootstrap.php';
require __dir__ . '/src/controllers/index.php';
src/core/bootstrap.php is stuff for composer and databases. But this is what src/controllers/index.php is:
<?php
session_start();
use App\UserTools\User;
use App\Core\Router;
$page = Router::load()->fetchPage();
include "src/controllers/{$page}.controller.php";
include "src/views/{$page}.view.php";
So, when users visit site.com, it goes to main since it's the home page. But, let's say, for example, they visit: site.com/about, then $page would be about and viola... routing. This was all taught to me on Laracasts, so excuse me if this seems... rudimentary.
The problem lies, when I visit site.com/api, it just shows me a blank page. Or, like, book?id=1 blank page. Or
Here is the nginx block from valet which tells the server what to do with files not found:
location / {
rewrite VALET_SERVER_PATH last;
}
How can I apply that to my site? I tried substituting VALET_SERVER_PATH with /var/www/html but I just got a Server Error 500.
Here is my current nginx block:
server {
server_name www.site.org;
return 301 $scheme://site.org$request_uri;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name site.org;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-site.org.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name site.org;
location / {
try_files $uri $uri/ /index.php;
}
location ~ /.well-known {
allow all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
It works. But only for the front page. Yes, I have HTTPS enabled and www traffic gets re-directed to non-www URI.

Make changes on the following directives:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}

Related

Nginx is sending CSP headers but I have no CSP anywhere?

As I am new to cloud hosting and server hosting (decided to take the jump from shared hosting) I can't pinpoint why this is happening.
Long story short I'm trying to get Google Fonts to load and neither Chrome nor Firefox are allowing it so I've begun to look up and understand the headers. I'm using php7.2 and Nginx 1.1.14 and both the default and my custom.conf file (domain file) have no CSPs loaded?
Any ideas how I can track this down?!
Refused to load the stylesheet 'https://fonts.googleapis.com/css?family=Averia+Serif+Libre' because it violates the following Content Security Policy directive: "style-src 'self' 'unsafe-inline'". Note that 'style-src-elem' was not explicitly set, so 'style-src' is used as a fallback.
But I don't have any CSP anywhere! So frustrated.
Here's my custom.conf:
server {
listen 80;
root /var/www/html/custom;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(eot|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And here's my default:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
EDIT: If it helps any I chose the "LEMP" option on Digital Ocean to create a custom setup? I've opened a ticket over there as well but it's been a couple days now.

NGINX access multiple sites same IP url

I would like to know how I can have several sites on Nginx and be able to access each of them with the same IP (without the domain, since I am doing tests in a laboratory locally).
I have the server on a separate PC and I access it remotely from my computer using the IP. Both are on the same LAN.
In the directory /var/www/ I have two sites 'nextcloud' and 'phpmyadmin'. I would like to be able to enter both by placing (for example) 192.168.1.14/nextcloud and 192.168.1.14/phpmyadmin. Or having any other project in the www directory.
I tried all the solutions I found, but none of them worked for me. When I enter phpmyadmin for example, it gives me to download the page instead of entering it.
Within /etc/nginx/sites-enabled I have the two files, one from nextcloud:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/nextcloud/;
index index.php index.html index.htm;
server_name localhost;
client_max_body_size 512M;
fastcgi_buffers 64 4K;
location / {
root /var/www/nextcloud;
rewrite ^ /index.php$request_uri;
}
location ~ ^/(?:build|tests|config|lib|3rdparty|templates|data)/ {
deny all;
}
location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
deny all;
}
location ~ ^/(?:index|remote|public|cron|core/ajax/update|status|ocs/v[12]|updater/.+|ocs-provider/.+)\.php(?:$|/) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_intercept_errors on;
fastcgi_request_buffering off;
}
location ~ ^/(?:updater|ocs-provider)(?:$|/) {
try_files $uri/ =404;
index index.php;
}
location ~ \.(?:css|js|woff|svg|gif)$ {
try_files $uri /index.php$request_uri;
add_header Cache-Control "public, max-age=15778463";
access_log off;
}
location ~ \.(?:png|html|ttf|ico|jpg|jpeg)$ {
try_files $uri /index.php$request_uri;
# Optional: Don't log access to other assets
access_log off;
}
}
And that of phpmyadmin:
server {
listen 80;
listen [::]:80;
root /var/www/phpmyadmin/;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
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;
}
# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}
Try creating two test folders in /var/www/ (test1 and test2), each with an index.html file inside and modifying the nginx default file, but it didn't work for me either
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
index index.html;
location / {
return 410; # Default root of site won't exist.
}
location /test1/ {
alias /var/www/test1/;
try_files $uri $uri/ =404;
# any additional configuration for non-static content
}
location /test2/ {
alias /var/www/test2/;
try_files $uri $uri/ =404;
# any additional configuration for non-static content
}
}
As I said, I tried different solutions. Another problem I had was that it only redirected me to nextcloud, although I put phpmyadmin in the url. And the previous one that I already mentioned, that when I enter, download the index.php. Thank you.
Sorry for my English.
Simple add nextcloud.my and phpmyadmin.my to your .hosts file and listen domain name in Nginx.
The option that you proposed can also be made to work, but it is full of bugs and difficulties can occur during the transfer to work server.

PHP not running on nginx with Debian 8

I just configured my Debian 8 server with nginx. I can browse html files. I use let's encrypt which works succesfully, also with redirecting http to https automatically.
What does not work is PHP. Also a simple info.php file with
<?php
phpinfo();
?>
does not work.
On browser client my error message is:
404 Not Found nginx/1.6.2
Nginx' error log shows this:
2018/05/29 19:22:57 [error] 1879#0: *1592 open() "/usr/share/nginx/html/info.php" failed (2: No such file or directory), client: ip_address, server: , request: "GET /info.php HTTP/1.1", host: "domain"
My nginx configuration is:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
server_name my-server.de www.my-server.de;
return 301 https://$server_name$request_uri;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ /.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-my-server.de.conf;
include snippets/ssl-params.conf;
}
Even if i move info.php to /usr/share/nginx/html then the client browser just downloads the info.php file.
I went through all steps in this guide. But still it isn't working. So how to fix that?
You have not added in SSL listen port 443, and ssl config of let's encrypt, please comment 301 redirect, test PHP, than go for SSL configuration see https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-debian-8
I have seen you have added SSL configuration, you need fix as below nginx configuration, after redirect, PHP should be configured on 443 not on 80.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name my-server.de www.my-server.de;
# Redirect to HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
include snippets/ssl-my-server.de.conf;
include snippets/ssl-params.conf;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ /.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
This is what I'm using:
upstream php {
server 127.0.0.1:9000;
server unix:/var/run/php5-fpm.sock down;
}
location ~* \.php$ {
root /var/www/html/;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 180;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_pass php;
fastcgi_index index.php;
}

nginx + wordpress in subfolder + debian configuration

In a subfolder on a domain I want to install a wordpress blog. I use nginx. The URL to access the blog should be like this: example.com/blog
site config looks as follows:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location /blog {
alias /var/www/example.comblog/html;
index index.php;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
location ~ /blog/.+\.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;
}
location ~ /\.ht {
deny all;
}
}
The wordpress files reside in the folder
/var/www/example.comblog/html. When accessing example.com/blog,
the browser shows a 404 error.
In /etc/php5/fpm/php.ini I adapted this: cgi.fix_pathinfo=0
nginx version: nginx/1.6.2
/var/log/nginx/error.log does not show anything of interest
UPDATE 1:
After setting error logging to debug, (among others) the following lines appear. Maybe this helps:
open index "/var/www/example.comblog/html/index.php"
internal redirect: "/blog/index.php?"
rewrite phase: 1
test location: "/blog"
test location: ~ "/blog/.+\.php$"
using configuration "/blog/.+\.php$"
http script var: "/blog/index.php"
trying to use file: "/blog/index.php" "/var/www/example.com/html/blog/index.php"
The internal redirect seems incorrect? And in the last line there should be /var/www/example.comblog/html/blog/index.php instead of /var/www/example.com/html/blog/index.php. I suspect this is the reason for the 404. Because the index.php does not exist at /var/www/example.com/html/blog/index.php.
Update 2:
Okay there seems to be a long standing issue with using alias together with try_files.

Why can't I access other page than default page on Magento?

Hi every one I am new to magento. I've been trying to install magento 1.9.0.0 on with nginx server on Ubuntu 14.04LTS but I can't get started. I can see default page see below
magento default page
but whenever I try to log in it, server fails with UNABLE TO CONNECT error.
here is my virtual host
server {
listen 80;
listen [::]:80;
server_name www.mymagento.com;
root /var/www/magento;
index index.php;
#need it to execute php
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
}
I already tried other solution like updating core_config_data web/unsecure/base_url and web/secure/base_url they contain my base url.
I tried reloading cache. there is nothing in nginx log neiher.
Thank you for helping me :-)
You have nothing to redirect URIs to the Magento common front handler. As a minimum you should add:
location / {
try_files $uri $uri/ /index.php;
}
But I suggest that you check this site for more.
Hi as Richard SMITH said, my virtual host was missing some lines to somehow redirect to front handler. As I have just begin to use Nginx, I won't be able to explain each lines but after strugling with Nginx this config works for me (I also needed to add self signed certificate to config to make it work).
server {
listen 80;
listen [::]:80;
listen 443 default ssl;
server_name www.mymagento.com;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
root /var/www/magento;
index index.php;
location / {
index index.html index.php;
autoindex on;
#If missing pass the URI to Magento's front handler
try_files $uri $uri/ #handler;
expires max;
}
#need it to execute php
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
include fastcgi.conf;
}
## Magento uses a common front handler
location #handler {
rewrite / /index.php;
}
}

Categories