NginX - path frameworks with index.php slash path slash path - php

I am building a docker collection that will eventually have the following containers (and more)
web (nginx)
proxy (reverse proxy nginx)
php-fpm
the web will have the allowance for several frameworks to be added via folders and subfolders
./folder1/folder2/codeigniter
./folder3/folder4/laravel
the folders for codeigniter and laravel are symlinks to a public folder
the index.php page works and shows the default routes without issue.
but when I try to get to a different page such as
/folder3/folder4/laravel/index.php/path/somwhere
I get a 404 error message.
I want to be able to do this without mapping an nginx location directive for EVERY FOLDER...
this is what my conf files looks like:
server {
listen 8100 default_server;
listen [::]:8100 default_server ipv6only=on;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
return 204;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri $uri/ index.php?$args;
fastcgi_pass php-fpm-56:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
what am I doing wrong here?
keep in mind, I want to use "/folder/index.php/path/path"
I DO NOT WANT
"/folder/path/path"
nor do I want to create a location entry for every new folder I create

i try to use codeifniter,
as an example
I will access
http://192.168.100.100/CI/CI_1/CI_1/index.php/main/index
after I tried this way, the results are according to what I attach
location /CI/CI_1/CI_1 {
autoindex on;
try_files $uri $uri/ /CI/CI_1/CI_1/index.php?/$request_uri;
}
Thanks

Related

PHP 7.4, NGINX, Laravel 8 - only shows index page, all routes show 404

I have put a Laravel 8 application on a AWS t2.nano Linux AMI ec2 instance. I would like to start up front by saying I have been at this for about a day now. I have tried a few configurations.
Here's some configurations I have tried:
The default nginx config file from the Laravel 8 documentation
https://laravel.com/docs/8.x/deployment#nginx
Another very similar stackoverflow question referenced here
Laravel on nginx says 404 for all routes except index
At the end of the day, I cannot get it to work properly. My index page loads, but any of the other routes end up at a 404 page. You can view the application here.
https://technology.dvemedia.com/
So here are some tech specs and the current state of my conf file.
Laravel - 8
PHP - 7.4
NGINX - 1.12.2
# HTTP
server {
listen 80;
listen [::]:80;
server_name technology;
return 301 https://$host$request_uri; # Redirect to www
}
server {
listen 80;
listen [::]:80;
server_name technology.dvemedia.com;
root /var/www/html/technology/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
What am I missing or doing wrong, because I cannot get it to route to save my life.
Try this:
## Nginx php-fpm Upstream
upstream dvemedia {
server unix:/var/run/php/php7.4-fpm.sock;
}
## Web Server Config
server
{
## Server Info
listen 80;
listen [::]:80;
server_name technology.dvemedia.com;
root /var/www/html/technology/public;
index index.html index.php;
## DocumentRoot setup
location / {
try_files $uri $uri/ #handler;
expires 30d;
}
## Disable .htaccess and other hidden files
location /. {
return 404;
}
## Rewrite all request to index
location #handler {
rewrite / /index.php;
}
## Execute PHP scripts
location ~ \.php$ {
try_files $uri = 404;
expires off;
fastcgi_pass dvemedia;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and put all your optimisation/tweaks (like fastcgi_buffers ...) in fastcgi_params file
I made a bad assumption by thinking my php-fpm socket would stay the same. After looking at the directory structure, my socket for 7.4 ended up being here.
fastcgi_pass unix:/var/run/php-fpm/www.sock;
That actually fixed it and everything worked. I gave bad information when I wrote the path for my socket was actually this.
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
#Latheesan answer would most likely have worked had I had given the correct information, minus the spelling mistake of course.

Laravel assets and routes returning 403 after deploying on production server using nginx

The routes and assets are loading properly on local server. However, after I deployed on production server using nginx, only the root url is working which is http://calculator.example.com. But all the assets are returning 403. Also when I try to access any routes for eg: http://calculator.example.com/page-1/ it is also returning 403.
Nginx config:
server {
listen 80;
server_name calculator.example.com;
root /var/www/html/calculator/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ .php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.(?!well-known).* {
deny all;
}
}
If you have directory indexing off, and is having this problem, it's probably because the try_files you are using has a directory option:
location / {
try_files $uri $uri/ /index.php?$query_string;
} ^ that is the issue
Remove it and it should work:
location / {
try_files $uri /index.php?$query_string;
}
Why this happens
TL;DR: This is caused because nginx will try to index the directory, and be blocked by itself. Throwing the error mentioned by OP.
try_files $uri $uri/ means, from the root directory, try the file pointed by the uri, if that does not exists, try a directory instead (hence the /). When nginx access a directory, it tries to index it and return the list of files inside it to the browser/client, however by default directory indexing is disabled, and so it returns the error "Nginx 403 error: directory index of [folder] is forbidden".
Directory indexing is controlled by the autoindex option: https://nginx.org/en/docs/http/ngx_http_autoindex_module.html
First, you need check if requests have been passed to app.
if 403 be returned in nginx, i think issue is user run nginx not have permission to access or execute code.
if 403 be returned in php app, please debug normally.
This issue got resolved after I removed the deny all for other location:
location ~ /.(?!well-known).* {
deny all;
}

Nginx RTMP dynamic stream name

I'm trying to create a simple RTMP/HLS server to be able to stream to and to view streams from, however, I am unable to change the name of the directory to which nginx-rtmp-module (module) saves the data.
Everything works just fine, I am able to stream to server and then view the HLS stream in browser, however, the only way to do so is to enter the Stream Key as the target folder on the server to get the m3u8 file.
Currently I have the following settings:
Stream key is generated on the server and then shown to the client
Client enters: rtmp://live.local/live as the endpoint and then inserts the key generated in step 1
Upon the request to start the stream, module calls on_publish to http://app.local/stream/start where at first, it checks if the key exists in the database, and if it is, it then creates new database entry for stream (with UUID as the primary key), receives the key obtained through entry creation and returns it to the script
Script takes the UUID obtained in step 3, then it uses SHA-512 algorithm to generate unique string (which was planned to be used as the directory name)
After hashing is performed, script returns 301 with location header
The stream itself is now renamed, however the folder in which files are stored is still the Stream Key instead of hashed value obtained in step 4 and returned in step 5.
Does anybody know on how to:
Rename stream (I guess I've done that already)
Rename directory for the stream files
OR is there a way to dynamically rewrite all requests, for example, client requests http://live.local/stream/here_is_the_hashed_uuid/index.m3u8, php takes the hash value, gets the list of active streams, hashes every streams' uuid and if matched, returns files from the other directory
PS: Just a small example of what I have now and what I want to achieve
Stream key: test_stream_key_user_1
Hashed key: 73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C3765BC53BB151C7F5B1431E
Current directory naming: /tmp/hls/test_stream_key_user_1/index.m3u8
Desired directory naming: /tmp/hls/73E179A6DB3796A3120319BFE80763427A2122253A5C1D347461268304B28CEB98CE3813DDF5FA8B7173937ED9386169FD3BF8E8C3765BC53BB151C7F5B1431E/index.m3u8
PPS: I thought that my nginx config files might help.
RTMP configuration
server {
listen 1935;
ping 30s;
notify_method get;
notify_update_timeout 10s;
application live {
live on;
hls on;
hls_nested on;
hls_path /tmp/hls;
hls_fragment 3;
hls_playlist_length 60;
on_publish http://app.local/api/stream/start;
on_done http://app.local/api/stream/stop;
on_update http://app.local/api/stream/update;
}
}
Server Configuration (APP)
server {
listen 80;
listen [::]:80;
root #replaced#;
index index.php index.html index.htm index.nginx-debian.html;
server_name app.local www.app.local;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# PHP-FPM Configuration Nginx
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
location /sitemap.xml {
try_files $uri $uri/ /index.php?$query_string;
}
location /login {
try_files $uri $uri/ /index.php?$query_string;
}
location /broadcasting {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
Server Configuration (LIVE)
server {
listen 80;
listen [::]:80;
root #replaced#;
index index.html index.htm index.nginx-debian.html;
server_name live.local www.live.local;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet original_stat.xsl;
}
location /control {
rtmp_control all;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /stream {
try_files $uri $uri/ /index.php?$query_string;
}
location /hls {
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Headers' 'Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Headers' 'Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
location /dash {
root /tmp;
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*' always;
}
}

How to fix CORS on Nginx?

I had a spa project which run frontend and backend using different port.
It can run successfully when I using php artisan serve.
But now I tried to run the laravel backend at nginx without php artisan serve.
# backend
server {
listen 3000 default_server;
listen [::]:3000 default_server;
root /usr/nextJs/nextTestBackend/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
}
After nginx start, 127.0.0.1:3000/api/GET/users can't access laravel backend.
It will shows the error:
Failed to load http://127.0.0.1:3000/api/GET/test: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access. The response had HTTP status code 500.
How to fix it?
Even though I put this inside location{} it still get the same error.
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
By the way the api at laravel also had CORS middleware.
Route::middleware('cors')->get('/GET/users', 'UserController#read');

How to deploy laravel with nginx using wildcard

I want to deploy my laravel application with nginx using wildcard domain.
But that not work correctly. I have this error:
Corrupted Content Error
The site at http://www.exemple.com/ has experienced a network protocol violation that cannot be repaired.
The page you are trying to view cannot be shown because an error in the data transmission was detected.
Please contact the website owners to inform them of this problem.
An example for my laravel routing
<?php
Route::group(['domain' => "{sub}.exemple.com"], function() {
// load site content
});
This is my nginx configuration:
server {
# Update max body size
client_max_body_size 20M;
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server ipv6only=on;
ssl on;
include /etc/nginx/snippets/self-signed.conf;
include /etc/nginx/snippets/ssl-params.conf;
# Route and app index
root /var/www/site/public;
index index.php index.html;
# Make site accessible from https://www.exemple.com
server_name ~^([a-z]+)\.exemple\.com$;
location / {
if ($http_x_forwarded_proto != "https") {
return 301 https://$1.exemple.com$request_uri;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SERVER_NAME $host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~* \.(?:ico|gif|jpe?g|png)$ {
expires 7d;
add_header Pragma public;
add_header Cache-Control "public";
access_log off;
}
location ~* \.(css|js|ttf)$ {
expires 1d;
access_log off;
add_header Cache-Control "public";
}
}

Categories