Configure NGinx to distribute laravel in a subfolder - php

I try to configure Nginx to distribute my laravel api on a subfolder of my domain
server {
listen 80;
server_name stackoverflow.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
charset utf-8;
location /dev {
alias /home/debian/www/stackoverflow/api/dev/public;
try_files $uri $uri/ /dev/index.php?$query_string;
location = /dev/favicon.ico { access_log off; log_not_found off; }
location = /dev/robots.txt { access_log off; log_not_found off; }
error_page 404 /dev/index.php;
error_log /home/debian/logs/nginx/error_log;
access_log /home/debian/logs/nginx/access_log;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I tried different configuration, with /dev on try_files, without /dev, but always same result "File not found" and in error log :
*1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 109.221.246.178, server: xxx, request: "GET /dev/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "xxx"
or
*1 open() "/home/debian/www/xxx/api/dev/public/login" failed (2: No such file or directory), client: xxx, server: xxx, request: "GET /dev/login HTTP/1.1", host: "xxx"
My poor knowledge on Nginx stop there. I know its about a file path issue, but can't understand where...
The configuration works perfectly when I try to set up on location / { }
Thanks a lot for your help !

This is my based conf when i put laravel app in domain subfolder, you can try to add your custom settings on it.
My laravel app root is in "subsite" folder, so you may change "subsite" to "dev" and test it.
location /subsite {
alias /var/www/project/subsite/public/;
try_files $uri $uri/ #subsite;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
}
}
location #subsite {
rewrite /subsite/(.*)$ /subsite/index.php?/$1 last;
}

Related

How to configure NGINX with php-fpm on mac

Been facing this issue now for quite a long time. New on nginx so need help
Basically Im trying to configure nginx settings to use my PHP projects. Before I was using Apache (MAMP) to run all my php proj. Now im shifting to nginx. I have configured all possible settings needed in nginx.conf file (have a look below). But still im facing rather the same issue and im not able to correctly address it for some reason.
...nginx default settings
nginx.conf
server{
listen 80;
error_log /usr/local/etc/nginx/log/freespinswizard-error.log;
access_log /usr/local/etc/nginx/log/freespinswizard-access.log;
access_log off;
#location / {
root /Applications/MAMP/htdocs/freespinswizard/;
index index.php index.html index.htm;
#}
#root /Applications/MAMP/htdocs/freespinswizard/;
#index index.php index.html index.htm;
server_name www.freespinswizard.com freespinswizard.com;
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml|otf)$ {
access_log off;
log_not_found off;
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ \.php$ {
try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass unix:/tmp/freespinswizard.sock;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
#fastcgi_buffer_size 128k;
#fastcgi_buffers 256 16k;
#fastcgi_busy_buffers_size 256k;
#fastcgi_temp_file_write_size 256k;
include fastcgi_params;
add_header X-Frame-Options SAMEORIGIN;
}
rewrite ^/offers/([^/\.]*)/?$ /page-handler.php?story-page-slug=$1 break;
rewrite ^/ab/([^/\.]*)/?$ /page-handler.php?group-key-name=$1 break;
if ($request_uri !~ "(^/scripts/)"){
rewrite ^/.*.php.* /page-handler.php break;
}
if ($request_uri !~ "(^/.*\..*)"){
rewrite ^(.*)$ /page-handler.php break;
}
}
......nginx default settings continued
Error im facing in log file regarding the website im trying to host
2020/11/04 20:14:49 [error] 21300#0: *1 kevent() reported that connect() failed (61:
Connection refused) while connecting to upstream, client: 127.0.0.1, server:
www.freespinswizard.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000",
host: "freespinswizard.com"
I have no clue what is wrong in my configurations. Please any kind of help would be appreciated, thnx in advance.

Every PHP error causes an nginx 500 error instead of showing PHP error data

I get a 502 server error from nginx on every PHP request that has an error in it. This is what shows up in the nginx log for every type of error:
[error] 394#0: *7 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: example.test, request: "GET /nova-api/users/lens/example-lens?search=&filters=W3siY2xhc3MiOiJBcHBcXE5vdmFcXEZpbHRlcnNcXExlbnNVc2VyRmlsdGVyIiwidmFsdWUiOnsiY29sdW1uIjoiMCIsIm9wZXJhdG9yIjoiPSIsImRhdGEiOiJoYXJ2ZXkifX1d&orderBy=&orderByDirection=desc&perPage=25&page=1&viaResource=&viaResourceId=&relationshipType= HTTP/2.0", upstream: "fastcgi://unix:/Users/user/.config/valet/valet.sock:", host: "example.test", referrer: "https://example.test/nova/resources/users/lens/example-lens"
If I run the same code on any other system, I actually get a PHP error in my laravel.log file.
I've tried increasing the number of max PHP children.
I've tried completely reinstalling nginx, PHP, and Laravel Valet.
My valet.conf nginx file which is auto-generated by Laravel Valet:
server {
listen 127.0.0.1:80 default_server;
root /;
charset utf-8;
client_max_body_size 128M;
location /41c270e4-5535-4daa-b23e-c269744c2f45/ {
internal;
alias /;
try_files $uri $uri/;
}
location / {
rewrite ^ "/Users/user/.composer/vendor/laravel/valet/server.php" last;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log "/Users/user/.config/valet/Log/nginx-error.log";
error_page 404 "/Users/user/.composer/vendor/laravel/valet/server.php";
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass "unix:/Users/user/.config/valet/valet.sock";
fastcgi_index "/Users/user/.composer/vendor/laravel/valet/server.php";
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/Users/user/.composer/vendor/laravel/valet/server.php";
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~ /\.ht {
deny all;
}
}
Manually removing valet by doing sudo rm -rf /Users/myuser/.config/valet/ and then valet install fixed the issue.

"Unable to open primary script: /usr/share/webapps/roundcubemail/webmail/index.php (No such file or directory)"

I have configured roundcubemail according to this archlinux wiki using nginx.
When I visit the /webmail using: https://mail.hackeac.com/webmail page (In the browser) renders: "No input file specified" and when I check on the logs at /var/log/nginx/roundcubemail_error.log I see
2017/12/11 13:44:10 [error] 5827#5827: *1 "/usr/share/webapps/roundcubemailwebmail/index.html" is not found (2: No such file or directory), client: 169.255.184.153, server: mail.hackeac.com, request: "GET /webmail/ HTTP/2.0", host: "mail.hackeac.com"
2017/12/11 13:44:19 [error] 5827#5827: *1 "/usr/share/webapps/roundcubemailwebmail/index.html" is not found (2: No such file or directory), client: 169.255.184.153, server: mail.hackeac.com, request: "GET /webmail/ HTTP/2.0", host: "mail.hackeac.com"
Nginx config:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name mail.hackeac.com;
ssl_certificate /etc/letsencrypt/live/hackeac.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/hackeac.com/privkey.pem;
location /webmail {
alias /usr/share/webapps/roundcubemail;
access_log /var/log/nginx/roundcube_access.log;
error_log /var/log/nginx/roundcube_error.log;
# Favicon
location ~ ^/webmail/favicon.ico$ {
root /usr/share/webapps/roundcubemail/skins/classic/images;
log_not_found off;
access_log off;
expires max;
}
# Robots file
location ~ ^/webmail/robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny Protected directories
location ~ ^/webmail/(config|temp|logs)/ {
deny all;
}
location ~ ^/webmail/(README|INSTALL|LICENSE|CHANGELOG|UPGRADING)$ {
deny all;
}
location ~ ^/webmail/(bin|SQL)/ {
deny all;
}
# Hide .md files
location ~ ^/webmail/(.+\.md)$ {
deny all;
}
# Hide all dot files
location ~ ^/webmail/\. {
deny all;
access_log off;
log_not_found off;
}
#Roundcube fastcgi config
location ~ /webmail(/.*\.php)$ {
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_split_path_info ^/webmail/(.+\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/webapps/roundcubemail$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PHP_VALUE open_basedir="/tmp/:/var/cache/roundcubemail:/usr/share/webapps/roundcubemail:/etc/webapps/roundcubemail:/usr/share/pear/:/var/log/roundcubemail";
}
}
}
Or (with better indentation) Here.
Please help.
You were using fastcgi_param SCRIPT_FILENAME /usr/share/webapps/roundcubemail$fastcgi_script_name; to compute the path to the script file. It is simpler to use $request_filename which computes the correct value with both root and alias directives.
For example:
location /webmail {
alias /usr/share/webapps/roundcubemail;
...
location ~ \.php$ {
include fastcgi.conf;
...
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
See this document for details.

Does nginx fastcgi_pass support variables?

I would like to use dynamic host resolution with nginx and fastcgi_pass.
When fastcgi_pass $wphost:9000; is set in the conf then nginx displays the error
[error] 7#7: *1 wordpress.docker could not be resolved (3: Host not found),
but when I set fastcgi_pass wordpress.docker:9000;it is working except for the fact the that after a wordpress restart nginx still points to an old ip.
server {
listen [::]:80;
include /etc/nginx/ssl/ssl.conf;
server_name app.domain.*;
root /var/www/html;
index index.php index.html index.htm;
resolver 172.17.42.1 valid=60s;
resolver_timeout 3s;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args; ## First attempt to serve request as file, then as directory, then fall back to index.html
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
set $wphost wordpress.docker;
# pass the PHP scripts to FastCGI server listening on wordpress.docker
location ~ \.php$ {
client_max_body_size 25M;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass $wphost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
log_not_found off;
expires 168h;
}
# deny access to . files, for security
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
I have different virtual host configuration where I use proxy_pass http://$hostname; and in this setup everything is working as expected and the host is found.
After trying different options I wonder if fastcgi_pass does support variables
It does work if you pass the upstream as a variable instead, for example:
upstream fastcgi_backend1 {
server php_node1:9000;
}
upstream fastcgi_backend2 {
server php_node2:9000;
}
server {
listen 80;
server_name _;
set $FASTCGI_BACKEND1 fastcgi_backend1;
set $FASTCGI_BACKEND2 fastcgi_backend2;
location ~ ^/site1/index.php$ {
fastcgi_pass $FASTCGI_BACKEND1;
}
location ~ ^/site2/index.php$ {
fastcgi_pass $FASTCGI_BACKEND2;
}
}

create a block in nginx for symfony2

server {
listen 80;
server_name myapp.local
root /home/jack/Documents/projects/php/myapp/web;
location / {
#try_files $uri $uri/ /app_dev.php?$query_string;
#try_files $uri /app_dev.php$is_args$args;
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
access_log /var/log/nginx/myapp-access.log;
error_log /var/log/nginx/myapp-error.log;
}
I've been trying to make this work but I couldn't and when I go to myapp.local I got No input file specified.. In the logs I have:
[error] 6867#0: *1 FastCGI sent in stderr: "Unable to open primary script: /usr/share/nginx/html/app.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: myapp.local, request: "GET /app.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "myapp.local"
UPDATE
It looks like it's trying to access /usr/share/nginx/html/app.php, why in the world is it doing that?
nginx -v : 1.4.6
Believe it or not guys, after 4 hours of debugging I realized that it was all about a poor missing semicolon after server_name statement. What a shame!

Categories