Drupal Sites Shows content of the Index.php file - php

Im trying to move a drupal site I started on my localhost to a server at home. The database is both exported from my localhost and stored on the server.
The content of the nginx.conf file is as follows
events {
worker_connections 768;
# multi_accept on;
}
http{
server {
listen 443 ssl;
######## S S L CONFIGURATIONS ##################
ssl_certificate /etc/ssl/Nov2021/STAR_site.edu.co.crt;
ssl_certificate_key /etc/ssl/Nov2021/site.edu.co.key;
access_log /var/log/nginx/KNH_nginx.vhost.access.log;
error_log /var/log/nginx/KNH_nginx.vhost.error.log;
root /var/www/html/arctic_kittiwake;
index index.php index.html index.htm;
###################################################
server_name site.edu.co
location / {
#try_files $uri $uri/ /index.php?q=$uri&$args;
try_files $uri /index.php?q=$uri$args;
}
location /site/ {
if (!-e $request_filename){
rewrite ^/site/(.*)$ /site/index.php break;
}
}
location ~ \.php$ {
fastcgi_index index.php;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
}
The directory where this file is stored is the /etc/nginx and the drupal site is stored in the /var/www/html/arctic_kittiwake/ directory.
I also have php7.4-fpm and mariadb-10.3 installed.

You are missing connection with php-fpm.
Example:
# In Drupal 8, we must also match new paths where the '.php' appears in
# the middle, such as update.php/selection. The rule we use is strict,
# and only allows this pattern with the update.php front controller.
# This allows legacy path aliases in the form of
# blog/index.php/legacy-path to continue to route to Drupal nodes. If
# you do not have any paths like that, then you might prefer to use a
# laxer rule, such as:
# location ~ \.php(/|$) {
# The laxer rule will continue to work if Drupal uses this new URL
# pattern with front controllers other than update.php in a future
# release.
location ~ '\.php$|^/update.php' {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
# Ensure the php file exists. Mitigates CVE-2019-11043
try_files $fastcgi_script_name =404;
# Security note: If you're running a version of PHP older than the
# latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
# See http://serverfault.com/q/627903/94922 for details.
include fastcgi_params;
# Block httpoxy attacks. See https://httpoxy.org/.
fastcgi_param HTTP_PROXY "";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param QUERY_STRING $query_string;
fastcgi_intercept_errors on;
# PHP 5 socket location.
#fastcgi_pass unix:/var/run/php5-fpm.sock;
# PHP 7 socket location.
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
Full example is here https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/

Related

Nginx no error page is showing, no matter what configuration I use

I'm trying to configure an error page in nginx for 500 and 502 error codes, I tried many different configuration options and solutions but none of them worked for me.
The issue itself is, that no matter how I do the configuration, I always get the generic Nginx error page with 502 bad gateway.
The following docker stack is running with these containers:
Nginx
MySQL
Composer
Azure CLI
PHP
A TYPO3 system is running behind the php/composer container.
I'm using Nginx instead of a Apache web server.
Below you can see my current nginx configuration.
server {
listen 80;
root /var/www/html/public;
index index.php index.htm index.html;
# Make site accessible from http://localhost/
server_name _;
# Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
sendfile off;
error_log /dev/stdout info;
access_log /var/log/nginx/access.log;
# NGINX - Provide error page
error_page 500 502 /error.html;
location = /error.html {
internal;
}
## provide a health check endpoint
location /healthcheck {
access_log off;
stub_status on;
keepalive_timeout 0; # Disable HTTP keepalive
return 200;
}
location / {
absolute_redirect off;
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass ${PHP_DOMAIN}:9000;
fastcgi_buffers 16 128k;
fastcgi_buffer_size 128k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_intercept_errors off;
# fastcgi_read_timeout should match max_execution_time in php.ini
fastcgi_read_timeout 600;
fastcgi_param SERVER_NAME $host;
fastcgi_cache_bypass $http_x_blackfire_query;
}
# Expire rules for static content
# Feed
location ~* \.(?:rss|atom)$ {
expires 1h;
}
# Media: images, icons, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# Prevent clients from accessing hidden files (starting with a dot)
# This is particularly important if you store .htpasswd files in the site hierarchy
# Access to `/.well-known/` is allowed.
# https://www.mnot.net/blog/2010/04/07/well-known
# https://tools.ietf.org/html/rfc5785
location ~* /\.(?!well-known\/) {
deny all;
}
# Prevent clients from accessing to backup/config/source files
location ~* (?:\.(?:bak|conf|dist|fla|in[ci]|log|psd|sh|sql|sw[op])|~)$ {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
# TYPO3 - Block access to composer files
location ~* composer\.(?:json|lock) {
deny all;
}
# TYPO3 - Block access to flexform files
location ~* flexform[^.]*\.xml {
deny all;
}
# TYPO3 - Block access to language files
location ~* locallang[^.]*\.(?:xml|xlf)$ {
deny all;
}
# TYPO3 - Block access to static typoscript files
location ~* ext_conf_template\.txt|ext_typoscript_constants\.(?:txt|typoscript)|ext_typoscript_setup\.(?:txt|typoscript) {
deny all;
}
# TYPO3 - Block access to miscellaneous protected files
location ~* /.*\.(?:bak|co?nf|cfg|ya?ml|ts|typoscript|dist|fla|in[ci]|log|sh|sql)$ {
deny all;
}
# TYPO3 - Block access to recycler and temporary directories
location ~ _(?:recycler|temp)_/ {
deny all;
}
# TYPO3 - Block access to configuration files stored in fileadmin
location ~ fileadmin/(?:templates)/.*\.(?:txt|ts|typoscript)$ {
deny all;
}
# TYPO3 - Block access to libaries, source and temporary compiled data
location ~ ^(?:vendor|typo3_src|typo3temp/var) {
deny all;
}
# TYPO3 - Block access to protected extension directories
location ~ (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ {
deny all;
}
if (!-e $request_filename) {
rewrite ^/(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ /$1.$3 last;
}
#Include development locations if needed
include /etc/nginx/conf.d/locations/*.conf;
}
I think the issue itself does not come from the configuration but from anywhere else but I don't know where.. I can't find the problem.
Hope u guys can help me, btw it's my first stack overflow question :D
EDIT:
Just added the configuration below to test the error codes but unfortunately I still get a 502 Bad Gateway, maybe a problem with the local setup. To my surprise the configured location for a healthcheck is working, just the error page not.
location /get_error {
return 500;
}
UPDATE:
The configuration itself was correct, I just deployed the changes made to the dev system and it just worked! I don't know why and where the issue was but it just won't work for my local dev environment.

Rewrite /ping to index.php/ping on nginx

I have started off with a docker image which has a processor for php files in place.
Requesting the normal index.php file goes without issue, but the routes I added are less accessible.
One of the routes for example is /time, accessing this is done (with the php builtin webserver) with
index.php/time, and it would return me the time. Now the docker I used has php working perfectly, just that I think one of the rules is breaking this functionality for me. tried a lot of changes, but cant seem to find the issue.
$app->get('/time', function ($request, $response, $args) {
// Current Unix timestamp
$seconds = time();
// Current Unix timestamp with microseconds
$milliseconds = microtime(true)*1000;
// Nanosecond precision
// This is default as per InfluxDB standard
$nanoseconds = $milliseconds*1000000;
// Return
return $response->withJson(array(
'seconds' => $seconds,
'milliseconds' => intval($milliseconds),
'nanoseconds' => intval($nanoseconds)
), 200);
});
This is the configuration of the nginx server I am serving in the docker. One of the things I have tried is adding the location /ping { line. But to no avail, /ping, /time keep redirecting to a 404 not found.
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/html/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name _;
# Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
sendfile off;
# Add stdout logging
error_log /dev/stdout info;
access_log /dev/stdout;
# Add option for x-forward-for (real ip when behind elb)
#real_ip_header X-Forwarded-For;
#set_real_ip_from 172.16.0.0/12;
location /ping {
try_files $uri $uri/ /index.php/ping;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
location = /404.html {
root /var/www/errors;
internal;
}
location ^~ /sad.svg {
alias /var/www/errors/sad.svg;
access_log off;
}
location ^~ /twitter.svg {
alias /var/www/errors/twitter.svg;
access_log off;
}
location ^~ /gitlab.svg {
alias /var/www/errors/gitlab.svg;
access_log off;
}
# pass the PHP scripts to FastCGI server listening on socket
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
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|webp|tiff|ttf|svg)$ {
expires 5d;
}
# deny access to . files, for security
#
location ~ /\. {
log_not_found off;
deny all;
}
location ^~ /.well-known {
allow all;
auth_basic off;
}
}

Wordpress Not Working On Nginx Server

You will have to bear with me here while I try and explain this the best I can.
I am working with a nginx server that I did not set up, I have very little knowledge of nginx. I have set up a new wordpress website which lives under the following url structure subsubdomain.subdomain.domain.com/website/ it is important that the full wordpress website is functional within the /website/ directory.
I have the site set up and the home page works perfectly when I navigate to subsubdomain.subdomain.domain.com/website/, but when I navigate to a subpage subsubdomain.subdomain.domain.com/website/resources/ the server throws File not found.
From my little knowledge of nginx I think this is a file permissions issue, I have logged into the server and run the following command sudo chmod 777 -R /path/to/website and also done sudo chown www:www -R /path/to/website to try and give full access. Unfortunately this has not worked either.
When checking the website access_log and error_log, they are empty. I then checked the nginx main log file and found the following error:
2018/05/31 04:07:42 [crit] 32426#0: *120 open() "/usr/share/nginx//var/www/sites-running/subsubdomain.subdomain.website.com/logs/nginx.access.log" failed (2: No such file or directory) while logging request, client: **.***.***.***, server: *.subdomain.domain.com, request: "GET /website/resources/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "subsubdomain.subdomain.domain.com", referrer: "http://subsubdomain.subdomain.domain.com/website/"
I'll be honest with you guys, this means nothing to me. All I can see is that it looks like the path to the log file is bad. So I went to my website nginx-vhost.conf file to see how it is defined and I have the following code:
access_log /var/www/sites-running/subsubdomain.subdomain.website.com/logs/nginx.access.log
Which looks all good to me.
So now I am stuck, I have no idea how to fix this so if anyone can make some sense of this and can help me out that would be amazing.
Cheers,
Luke.
UPDATE
I have just run nginx -V and noticed that there is a value called prefix, here is the value:
--prefix=/usr/share/nginx
It looks like this could be my problem but I have no idea what this is, how it is used and do not know the damage i could cause if I change it.
UPDATE
Here is my website nginx-vhost.conf file.
# Nginx configuration for Website
# This is for development purposes
server{
listen 80;
server_name subsubdomain.subdomain.domain.com;
set $site_root "/var/www/sites-available/$host";
set $public_html "$site_root/public_html";
set $logs_dir "$site_root/logs";
set $nginx_root "$site_root/webapps/ROOT";
root $nginx_root;
error_log /var/www/sites-available/subsubdomain.subdomain.domain.com/logs/nginx.error.log;
access_log /var/www/sites-available/subsubdomain.subdomain.domain.com/logs/nginx.access.log main;
index index.php;
#default_type text/html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#add_header 'Access-Control-Allow-Origin' "*";
# ------------------------------------------------------
#
# static resources routing for version control on assets
#
# ------------------------------------------------------
#location ~ ^/static/([^/]+)/(content|resources)/(.*)$ {
# alias $public_html/$2/$3;
#}
#location ~ ^/content/(.*)$ {
# alias $public_html/content/$1;
#}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location /wp-admin {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /wp-admin/index.php?$args;
}
# ----------------------------------------
#
# PHP
#
# ----------------------------------------
location ~ \.php {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SERVER_NAME $host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param CONTENT_ROOT $public_html/content;
fastcgi_param CONTENT_UPLOAD_DIR $public_html/content;
fastcgi_param LOGS_ROOT $logs_dir;
fastcgi_param app.profile staging;
fastcgi_param APP_MODE staging;
fastcgi_param DB_NAME **********;
fastcgi_param DB_USER **********;
fastcgi_param DB_PASS **********;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
Here is my main nginx.conf file
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server_names_hash_bucket_size 128;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
client_max_body_size 100m;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80 default_server;
server_name _;
root /usr/share/nginx/html;
#root /var/www/sites-running/nginx-default;
#index index.html index.htm;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.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$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
# 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;
# location / {
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# root html;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# }
#}
}
Thanks to everyone for their help. I have finally managed to figure out what was going wrong. I needed to update my conf file to have a second location statement which looked inside of the /website/ folder.
like so:
location /website/ {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/website/ /website/index.php?$args;
}
Here is my full nginx-vhost.conf file:
# Nginx configuration for Website
# This is for development purposes
server{
listen 80;
server_name subsubdomain.subdomain.domain.com;
set $site_root "/var/www/sites-available/$host";
set $public_html "$site_root/public_html";
set $logs_dir "$site_root/logs";
set $nginx_root "$site_root/webapps/ROOT";
root $nginx_root;
error_log /var/www/sites-available/subsubdomain.subdomain.domain.com/logs/nginx.error.log;
access_log /var/www/sites-available/subsubdomain.subdomain.domain.com/logs/nginx.access.log main;
index index.php;
#default_type text/html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
#add_header 'Access-Control-Allow-Origin' "*";
# ------------------------------------------------------
#
# static resources routing for version control on assets
#
# ------------------------------------------------------
#location ~ ^/static/([^/]+)/(content|resources)/(.*)$ {
# alias $public_html/$2/$3;
#}
#location ~ ^/content/(.*)$ {
# alias $public_html/content/$1;
#}
location /website/ {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/website/ /website/index.php?$args;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location /wp-admin {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /wp-admin/index.php?$args;
}
# ----------------------------------------
#
# PHP
#
# ----------------------------------------
location ~ \.php {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include /etc/nginx/fastcgi_params;
fastcgi_intercept_errors off;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SERVER_NAME $host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param CONTENT_ROOT $public_html/content;
fastcgi_param CONTENT_UPLOAD_DIR $public_html/content;
fastcgi_param LOGS_ROOT $logs_dir;
fastcgi_param app.profile staging;
fastcgi_param APP_MODE staging;
fastcgi_param DB_NAME **********;
fastcgi_param DB_USER **********;;
fastcgi_param DB_PASS **********;;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}

Nginx - Redirecting all request including .php to single PHP script?

I've been Googling this for a while but can't seem to find a solution.
At the moment I have a config file setup on Nginx to send all requests regardless of file extension to a single index.php file. However, it ignores requests ending with .php and will throw a 404 if it's not there or, try to execute it if it is.
How can I configure Nginx to send .php requests to the index.php file too so I can use it to handle all file requests, not just non-PHP files?
My config file currently looks like the following:
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /somecrt.crt;
ssl_certificate_key /somekey.key;
root /sites/;
index index.php;
server_name somesite.net;
access_log /sites/logs/access.log;
error_log /sites/logs/error.log;
location ~ /\. { deny all; }
location / {
# First attempt to serve request as file, then
# as directory then fall back to index.php
try_files $uri $uri/ /index.php?$args;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php?$args =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
After some more Googling "nginx: Map single static URL to a PHP file" helped me figure out the solution. So the new config is now:
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /somecrt.crt;
ssl_certificate_key /somekey.key;
root /sites/;
index index.php;
server_name somesite.net;
access_log /sites/logs/access.log;
error_log /sites/logs/error.log;
location ~ /\. { deny all; }
location / {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
With this config, all requests will be sent to the single index.php.
Of course, this will include static files such as image files which will probably impact Nginx server performance. In that case, you might want to add another location block before it if you want to exclude certain kind of requests.
For example, to exclude jpgs and gifs:
location ~ \.(jpg|gif) {
try_files $uri =404;
}

How to properly configure alias directive in nginx?

I have been trying to configure multiple webapp on my nginx webserver but I can't get working one Laravel app that requires $document_root set to laravel public folder.
I am currently trying to configure it using alias directive but for an obscure reason this doesn't work. Here is what I am trying to do.
# Default server configuration
#
server {
listen 80;
# SSL configuration
#
listen 443 ssl;
error_log /var/log/nginx/error.log warn;
ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
set $root_path '/var/www/html';
root $root_path;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name localhost;
location /paperwork {
alias /var/www/html/paperwork/frontend/public;
try_files $uri $uri/;
#location ~ \.php {
# fastcgi_split_path_info ^(.+\.php)(.*)$;
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# include /etc/nginx/fastcgi_params;
# #fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
# #fastcgi_intercept_errors on;
#}
}
#location #paperwork {
# rewrite /paperwork/(.*)$ /paperwork/index.php/$1 last;
#}
location / {
}
location /wallabag {
try_files $uri $uri/ /index.php;
}
location /laverna {
try_files $uri/ /index.php;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
location ~ /\.ht {
deny all;
}
}
To test my "alias" config I put a 'test.php' files in /var/www/html/paperwork/frontend/public/test.php and tried to access it via https://IP/paperwork/test.php. I get a 404 error and nothing in nginx error log.
If I try https://IP/paperwork/frontend/public/test.php in browser it displays the test.php file without errors.
Nothing change if I uncomment try_files line in php location.
If I copy test.php to /var/www/html/paperwork/test2.php and access to https://IP/paperwork/test2.php the file is displayed without errors so I can see here that alias is not working as there is not a test2.php in paperwork public directory.
I can have a different behaviour if I uncomment php location inside paperwork location. With this, requests like https://IP/paperwork/test.php do not display a 404 but a blank screen.
I have been through a lot of forums / questions related to this but I couldn't get a working config for a simple task like displaying test.php...
Thanks !
I found the solution. It seems that a wrong request was sent for php files. When alias is used it is recommend to use $request_filename instead of $fastcgi_script_name.
Here is my location block :
location /paperwork {
alias /var/www/html/paperwork/frontend/public;
#try_files $uri $uri/;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_intercept_errors on;
}
}
This solved my problem for my 'test.php' file which is now executed while reaching https://IP/paperwork/test.php. So alias is working and php is well executed.
I still have a problem when trying to reach 'index.php' (which is my laravel app index). File is found but instead of executing it is downloaded. So when I reach https://IP/paperwork/index.php I get a login file downloaded which is index.php file. I get same behaviour if I try /paperwork/index.php/login or /paperwork/login.
try this:
location /api/ {
index index.php index.html index.htm;
alias /app/www/;
location ~* "\.php$" {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/

Categories