I try to move my Yii2 app from using apache to nginx. I'm using Yii2 advanced template on a single domain, so I follow the solution from this link creating a symlink from frontend/web to the backend/web. This solution works fine when I'm using apache, but when I try to use nginx I can't open the backend app.
The frontend app works fine, but when I try to open the backend app using app.local/belakang, it is redirected to app.local/belakang/site/login, but still stuck in the frontend app and therefore giving a [yii\web\HttpException:404] exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "belakang/site/login".'
Here is the symlinks that I have on the frontend/web directory :
lrwxrwxrwx 1 vagrant vagrant 17 Jun 24 14:03 belakang -> ../../backend/web
and this is the nginx vhost config that I use:
server {
charset utf-8;
client_max_body_size 128M;
listen 80; ## listen for ipv4
server_name app.local;
root /var/www/html/frontend/web;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
location ~* /\. {
deny all;
}
}
Can anyone help so that I can open the backend app from app.local/belakang ?
Fix it myself by adding config below
location ^~ /belakang {
try_files $uri $uri/ /belakang/index.php$is_args$args;
location ~ /\.(ht|svn|git) {
deny all;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php/php5-fpm.sock;
try_files $uri =404;
}
}
Related
Disclaimer: I'm not a sys admin :)
I have the following setup:
- react pwa in /var/www/react/build
- php application under /var/www/html
- site url mysite.loc
I want all requests except mysite.loc/fapi/* and mysite.loc/api/* to be handled by react app and (fapi|api)/* by php app.
I am trying with the following nginx config:
server {
listen 80;
listen [::]:80;
server_name mysite.loc;
root /var/www/react/build;
index index.html index.php;
location ^~ /fapi {
rewrite ^/fapi/?(.*)$ /html/fapi/$1 last;
}
location ^~ /api {
rewrite ^/api/?(.*)$ /html/api/$1 last;
}
location ^~ /html {
index index.php;
root /var/www;
try_files $uri $uri/ /index.php$is_args$args;
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 SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
}
}
when accessing mysite.loc/fapi/user/get this config returns the following error:
"/var/www/react/build/index.php" failed (2: No such file or directory)
what I was expecting was that the nginx would send request to /var/www/html/fapi/index.php. Not ideal but something I can work with.. Ideally I would like the request to go to /var/www/html/index.php, request being /fapi/user/get.
Can you point me into the right direction?
Thanks.
I'm using Ubuntu 16.04, Nginx 1.10.3 and PHP 7.0. The example PHP applications are CodeIgniter 3.1.5.
I am trying to run a static page at the root of my site www.example.com(works) with multiple other CodeIgniter applications each running in subdirectories at www.example.com/client-a, www.example.com/client-b, etc.
The static page at my root runs fine, however when redirecting to the apps at the sub directories none of the stylesheets and scripts get loaded resulting in 404 errors. The routing works though.
The application files of the subsequent apps don't exist within one another.The application root exists in /var/www/example/public_html while the "nested" applications lives in /var/www/client_a/public_html, /var/www/client_b/public_html etc.
Here is my Nginx server block:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /var/www/example/public_html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /client-a {
alias /var/www/client_a/public_html;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
location #nested {
rewrite /client_a/(.*)$ /client_a/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location ~ /\.ht {
deny all;
}
}
I have installed the Homestead VM and setup the Moodle installation folder on my Mac (OSX Yosemite). I also created the 'moodledata' folder and gave it the permissions 0777 as well as the folder 'moodledata/sessions' via my system command line (I tried doing this via SSH inside the VM but it didn't appear to change the permissions). However checking the permissions after doing it via my system showed the folder was writable from inside the VM.
I then moved on to the installation which ran through and created the DB tables and did the check which showed 2 check warnings:
Intl and xmlrpc to check
I don't believe these are essential for initial installation so carried on. It is when I get to the admin user creation where I am getting a problem. The page (/user/editadvanced.php?id=2) stops loading any images and when I post the form I get an error: 'Incorrect sesskey submitted, form not accepted!'
I thought this could be down to the session not being writable in the moodledata folder but as I have checked that now I am out of ideas!
I have attached a couple of screenshots.
Many thanks, Mike.
Ok after a good few days of head scratching I fixed my own issue by editing the NGINX config file. Below is what it was by default:
server {
listen 80;
server_name example.com;
root /home/forge/example.com;
# FORGE SSL (DO NOT REMOVE!)
# ssl_certificate;
# ssl_certificate_key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
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; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
And this is what I changed it to and it now works:
server {
listen 80;
server_name example.com; #REPLACE SERVER NAME
root /var/www/example.com/www/; #REPLACE MOODLE INSTALL PATH
error_log /var/www/example.com/log/example.com_errors.log; #REPLACE MOODLE ERROR LOG PATH
access_log /var/www/example.com/log/example.com_access.log; #REPLACE MOODLE ACCESS LOG PATH
rewrite ^/(.*\.php)(/)(.*)$ /$1?file=/$3 last;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php;
}
fastcgi_intercept_errors on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I haven't had time to see which part/parts of the above config fixed the issue, maybe someone who knows can see straight away? I suspect it could be the rewrite rule? Either way I hope this helps someone else in the future and I am really happy to get this working!
I can confirm is just the rewrite part for that specific config file, although in the Moodle Nginx page it's not documented that way.
My guess is that the location ~ [^/]\.php(/|$) { part is doing the same thing as the rewrite rule rewrite ^/(.*\.php)(/)(.*)$ /$1?file=/$3 last; and the location ~ \.php$ { directive. Will need to make some test changing the location directive to see if that works as well.
I'm having an issue where when I go to the /public directory it shows the Laravel app as normal, but navigating away to any other page results in it saying
No input file specified.
I am using an Nginx server with PHP 5.5.9 FPM.
I've scoured google for the last 4 hours or so, looking at every tutorial and stackoverflow page for rewriting issues in Laravel however they all yield the same result.
I've even set all the files and folders to 777 so I could see if it was some sort of permissions issue. I've checked the Laravel config and it's all set, I've no idea what is wrong.
Can anyone point me in the right direction?
The last config I tried is below:
server {
listen 80 default_server;
root /usr/share/sites/base;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
I have also tried many others such as:
server {
listen 80;
server_name domain.com;
root /usr/share/sites/base;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~* \.php$ {
# Server PHP config.
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
The error "No input files specified" will nearly always be related to the fact that the wrong path was sent to php.
Looking at your 'last config tried' I can see that fastcgi_param SCRIPT_FILENAMEis not defined in your php location. You should first begin by defining it in the location :
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
}
Furthermore you say that you can reach the app so this means that index.php is working but not when you change page. So the problem should also come from /index.php?$args. Indeed, using this line if I try to reach yourserver.com/test and if 'test' is not a file in your root path nginx will then try request /index.php? (I had this probem). You should try only with /index.php.
EDIT : The solution was that root directive should point to the Laravel public folder, in that case /usr/share/sites/base/public.
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/