Problem with switching PHP code from Apache to Nginx - php

On Apache we used to have a URL like MyUrl.com/UserId and in the root index.php code executed that got the request_uri that had the UserId. In Nginx instead it looks for a folder called UserId which doesn’t exist. How can I modify the Nginx config to still use the root index.php file instead of look for a non existent folder?
Here is my current nginx config:
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/before/*;
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name MyUrl.com;
root /home/forge/MyUrl.com;
rewrite_log on;
# FORGE SSL (DO NOT REMOVE!)
ssl_certificate /etc/nginx/ssl/MyUrl.com/766650/server.crt;
ssl_certificate_key /etc/nginx/ssl/MyUrl.com/766650/server.key;
ssl_protocols TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/dhparams.pem;
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;
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/server/*;
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 /var/log/nginx/MyUrl.com-access.log combined;
error_log /var/log/nginx/MyUrl.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 180;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
location /api/v1/ {
rewrite_log on;
index index.php;
fastcgi_index index.php;
error_page 405 =200 $uri;
if (!-e $request_filename){
rewrite /api/v1/(.*)$ /api/v1/api.php?request=$1 break;
}
}
client_max_body_size 128M;
}
# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/MyUrl.com/after/*;

Apache and Nginx use different configuration formats. You need to convert what is currently in your .htaccess file into a format that Nginx understands.
This is an example of an Apache .htaccess configuration:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
In Nginx it could look something like this:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Take a look at these questions for more details:
Converting .htaccess to nginx (mod_rewrite)
Convert htaccess to nginx rewrite
Also this tool might help with the conversion:
https://winginx.com/en/htaccess

Related

Why in the global server variable, the query_param cell is not valid

I am trying to set up email verification, but because of wrong configuration of either, server or php I always get 403 Invalid signature error. If you know how to solve this problem I would be very grateful. All the solutions I googled don't work for me.
my route with params:
https://mysite.lo/email/verify/1001/82f42f0bbc6880958a68b56159cb7cbf96199ddf?expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
PHP DEBUG
$request->server->get('QUERY_STRING')
output:
/email/verify/1001/82f42f0bbc6880958a68b56159cb7cbf96199ddf&expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
enter image description here
But there should be another output, starting from ? to the end, for example:
expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
Or am I misunderstanding something?
Anyway, I don't understand why this is happening.
The nginx settings are below.
server {
listen 80;
listen 443 ssl;
listen [::]:80;
server_name mysite.lo *.mysite.lo;
ssl_certificate /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
access_log /var/www/mysite/mpa/storage/logs/nginx_access.log;
error_log /var/www/mysite/mpa/storage/logs/nginx_error.log;
root /var/www/mysite/mpa/public;
index index.php;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
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;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
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;
}
}
This is the local environment.
I am using: laradock php 8
It is your rewrite ^/(.*)$ /index.php?/$1 last; rule adds an original URI as the first query argument. As rewrite directive documentation says:
If a replacement string includes the new request arguments, the previous request arguments are appended after them.
You can avoid adding an original URI as the first query argument specifically for the /email/verify/ route:
if (!-e $request_filename)
{
# do not append an original URI to this route
rewrite ^/email/verify/ /index.php last;
# but append it to everything else
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}

Change root directory based on request url

We have a website where we are using a Laravel for the part available to the end-user, and an older legacy codebase for the admin panel.
Currently root is the public folder for the currently deployed version.
The website folder structure looks something like this:
admin/
|____index.php <-- desired entrypoint for admin-related requests
app/
public/
|____index.php <-- main entrypoint for website
resources/
routes/
So, when someone wants to access the admin panel they go to example.com/admin
Here's the current nginx configs file we are using.
example.com
server {
server_name example.com;
root /var/www/example.com/current/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /admin {
proxy_pass http://localhost:3000;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
example.com/admin
server {
listen 3000;
server_name example.com;
root /var/www/example.com/current/admin;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index 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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I'm not very well versed in nginx server configuration and would love som assistance on this.
EDIT 1
Config file using map:
EDIT 2
Updated the config below with the changes made to make it work.
map $uri $siteroot {
# This didn't work, per the accepted answer.
# ^/admin /var/www/example.com/current/admin;
# This works great!
^/admin /var/www/example.com/current;
default /var/www/example.com/current/public;
}
server {
server_name example.com;
root $siteroot;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.com;
return 404; # managed by Certbot
}
If you try changing root directory, Nginx will shows not found error.
Because, Nginx tries find file with request path even you changed root.
For example, If you change root directory for /admin path to /app/admin, Nginx will find file in /app/admin/admin. That's why you can't reach file.
Try insert rewrite /admin(.*) $1 break; line at /admin location block and set root path, or use alias expression.
I would use map directive for this, something like
map $uri $siteroot {
~^/admin /var/www/example.com/current/admin;
default /var/www/example.com/current/public;
}
server {
server_name example.com;
root $siteroot;
...
}

Site is not executing PHP code when Migrated from Apache to Nginx server

Recently I migrated the site from the Apache server to the Nginx server, On this Nginx server there is already another PHP site is hosted, but only the PHP code of this migrated site is not executing.
Below is a conf file for this site. When I try to run the site on the browser only Html code is displaying and PHP code is not executing.
server {
listen 80;
server_name xyzorg.in;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name xyzorg.in;
ssl on;
ssl_certificate /etc/pki/tls/certs/xyzorg_in.pem;
ssl_certificate_key /etc/pki/tls/certs/xyzorg_in.key;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.1 TLSv1.2; # Dropping
root /usr/local/www/site;
index index.html index.php report.html;
# redirect server error pages to the static page /50x.html
location / {
try_files $uri $uri?$args $uri/ /index.php?$uri&$args /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /favicon.ico { log_not_found off; access_log off;}
location = /robots.txt { log_not_found off; access_log off;}
location ~ /.well-known { allow all; }
}
In the apache server, there was a .htaccess file with the below code
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^xyzorg.in [NC]
RewriteRule ^(.*)$ http://www.xyzorg.in/$1 [L,R=301]
RewriteCond %{REQUEST_URI} index\.html
RewriteRule ^(.*)index\.html$ /$1 [R=301,L]

Laravel routes not found after nginx install

After I changed ICG to nginx all routes except index page does not work.
Laravel Config:
#/etc/nginx/sites-enabled/laravel
server {
listen 80;
root /var/www/home;
index index.php;
server_name 192.168.178.71;
access_log /var/www/home/storage/app/logs/laravel-nginx-access.log;
error_log /var/www/home/storage/app/logs/laravel-nginx-error.log error;
location /home {
root /home/public;
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
# ERROR
error_page 404 /index.php;
# DENY HTACCESS
location ~ /\.ht {
deny all;
}
}
Default config:
# /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name 192.168.178.71 localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ index.php?$query_string;
autoindex on;
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
}
location ~ \.php$ {
#try_files $uri /index.php =404;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
my nginx config
#/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
disable_symlinks off;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
What I tried:
/var/www/home# (home folder is laravel folder)
sudo chown -R www-data:www-data *
/var/www/home#
sudo chown -R root:root *
also I tried to change
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;
try_files $uri $uri/ /index.php;
php artisan cache:clear
Mostly questions in google i have read, but nothing helps me.
My phpinfo - link
This is the correct basic config for Laravel and Nginx:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT:
Instead of:
fastcgi_pass unix:/var/run/php5-fpm.sock;
As of November 2018, as PHP 7.2 is out, it would be:
fastcgi_pass unix:/var/run/php7.2-fpm.sock;
When I sent parameters by get I did not recognize them, I just have to activate the following:
try_files $uri $uri/ /index.php$is_args$args;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I had same problem after updating few lines nginx working fine..
It's for windows ( change root acording your file system )
1.root html/laravel; #Update Here - add project folder name after html
2.try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
server {
listen 80; # IPv4
server_name localhost;
## Parametrization using hostname of access and log filenames.
access_log logs/localhost_access.log;
error_log logs/localhost_error.log;
## Root and index files.
root html/laravel; #Update Here - add project folder name after html
index index.php index.html index.htm;
## If no favicon exists return a 204 (no content error).
location = /favicon.ico {
try_files $uri =204;
log_not_found off;
access_log off;
}
## Don't log robots.txt requests.
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Try the requested URI as files before handling it to PHP.
location / {
try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
## Regular PHP processing.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_processes;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
## Static files
location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
expires max;
log_not_found off;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
}
## Keep a tab on the 'big' static files.
location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
expires 30d;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
}
} # / location
}
I had the same issue, but updating the default configuration made it work.
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location / {
try_files $uri $uri/ #rewrite;
}
Let me know if this worked for you or not.
sudo service nginx restart after changing the configuration.
Try it, work for me.
sudo nano /etc/nginx/sites-enabled/default
and then sudo systemctl reload nginx
server {
listen 80;
server_name _ midominioexample.com www.midominioexample.com;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index 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.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I was also getting the same error of Routes not working on Nginx on my Ubuntu 16.04
To solve Routes problem, i tried the following code and its just working fine for me.
Open project conf file using following command
sudo nano /etc/nginx/sites-available/projectname
Then do the following changes in this file
server {
listen 80;
listen [::]:80;
root /var/www/project_name/public;
server_name server_name;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Important thing is to change the try_files in location block.
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I found that this solved my problem with Laravel routing.
I nested the location ~ .php$ inside location /.
Example:
server{
listen 9000;
server_name _;
root /var/www/myapp/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
}
As of 03/2022 and the current version of Laragon (5.0.0) I spent a lot of time to figure out why I can't open any link except index.php that I have configured in web.php for the route:list.
Because I just wanted to turn off SSL and turn it back on. It seems this causes to reset all your config files.
It seems that Laragon by default adds those lines:
# Access Restrictions
allow 127.0.0.1;
deny all;
I have put a # in front of deny to uncomment it and it worked again, like this:
# Access Restrictions
allow 127.0.0.1;
#deny all;

nginx and owncloud in subfolder

I want to have a owncloud instance in a subfolder on my nginx server. But I have problems with some of the files requested by opwncloud (it seems css and js don't load).
Here is the nginx conf file for this virtual host :
server {
listen 80;
server_name blackblock.22decembre.eu;
return 301 https://blackblock.22decembre.eu$request_uri;
}
server {
listen 443 default_server ssl;
server_name blackblock.22decembre.eu;
root /srv/www/blackblock/;
access_log /var/log/nginx/blackblock.access.log;
error_log /var/log/nginx/blackblock.errors.log;
index index.html index.php;
# This block will catch static file requests, such as images, css, js
# The : prefix is a "non-capturing" mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(:ico|css|js|gif|jpeg|png)$ {
# Some basic cache-control for static files to be sent to the browser
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# remove the robots line if you want to use wordpress" virtual robots.txt
# location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
# this prevents hidden files (beginning with a period) from being served
location ~ /\. { access_log off; log_not_found off; deny all; }
#location ~ ^(?<script_name>.+?\.php)(?<path_info>/.*)?$ {
location ~ \.php {
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
fastcgi_pass unix:/run/php5-fpm.sock;
include fastcgi_params;
}
location /roundcube/program/js/tiny_mce/ { alias /usr/share/tinymce/www/; }
location /roundcube/(config|temp|logs) { deny all;}
##### owncloud
location ~ /owncloud/ {
root /srv/www/blackblock/owncloud/;
try_files $uri $uri/ index.php;
#client_max_body_size 10G; # set max upload size
#fastcgi_buffers 64 4K;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
location ~ ^/remote.php(/.*)$ {
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_pass unix:/run/php5-fpm.sock;
include fastcgi_params;
}
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location ~ ^/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
# The following 2 rules are only needed with webfinger
rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/.well-known/caldav /remote.php/caldav/ redirect;
rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;
# Optional: set long EXPIRES header on static assets
#location ~* ^.+\.(jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
# expires 30d;
# Optional: Don't log access to assets
# access_log off;
# }
}
##### torrent (not related to owncloud, flask application)
location = /flask-torrent { rewrite ^ /flask-torrent/ last; }
}
I can't find why owncloud doesn't load correctly !
You can have a look at the website, I feel fine and secured for that : https://blackblock.22decembre.eu/owncloud/ (cacert certificates).
If I launch a specific virtual host for owncloud, it works perfectly, but I don't want, I prefer it in a subfolder of this host (blackblock) !
The reason why ownCloud doesn't work in a subfolder with nginx is that nginx, by default, doesn't include the subfolder in the parameter SCRIPT_NAME. If ownCloud is at domain.tld/owncloud/index.php, it expects $_SERVER['SCRIPT_NAME'] to be /owncloud/index.php, but nginx by default (if you include fastcgi_params;) sets it to index.php. The solution is to override the behaviour: add fastcgi_param SCRIPT_NAME /owncloud/$fastcgi_script_name; to the php-location-block in the nginx conf file.
Relevant parts of my nginx configuration file follow. Please note that I haven't tested it completely; on the first look it seems to work though. My System: nginx 1.2.1 and php 5.4.4 on Debian Wheezy 64 bit.
location /owncloud/ {
alias /var/www/owncloud/;
location ~ ^/owncloud/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
rewrite ^/owncloud/caldav(.*)$ /owncloud/remote.php/caldav$1 redirect;
rewrite ^/owncloud/carddav(.*)$ /owncloud/remote.php/carddav$1 redirect;
rewrite ^/owncloud/webdav(.*)$ /owncloud/remote.php/webdav$1 redirect;
rewrite ^/owncloud/.well-known/host-meta /owncloud/public.php?service=host-meta last;
rewrite ^/owncloud/.well-known/host-meta.json /owncloud/public.php?service=host-meta-json last;
rewrite ^/owncloud/.well-known/carddav /owncloud/remote.php/carddav/ redirect;
rewrite ^/owncloud/.well-known/caldav /owncloud/remote.php/caldav/ redirect;
rewrite ^/owncloud/apps/([^/]*)/(.*\.(css|php))$ /owncloud/index.php?app=$1&getfile=$2 last;
rewrite ^(/owncloud/core/doc/[^\/]+/)$ $1/index.html;
try_files $uri $uri/ index.php;
location ~ ^/owncloud/(.+?\.php)/? { # note the question mark here and in the next line!
fastcgi_split_path_info ^/owncloud/(.+?\.php)(/?.*)$;
set $path_info $fastcgi_path_info; # workaround for bug: try_files resets fastcgi_path_info for some reason.
try_files $fastcgi_script_name = 404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS on;
fastcgi_param SCRIPT_NAME /owncloud/$fastcgi_script_name; # !!!
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
None of the other answers worked for me, I finally got a working solution from this blog:
http://www.aelog.org/install-owncloud-in-a-subdirectory-using-nginx/
Here's a version:
server {
listen 80;
server_name example.com;
# enforce https
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/ssl/nginx/cloud.example.com.crt;
ssl_certificate_key /etc/ssl/nginx/cloud.example.com.key;
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
# Path to the root of your website (one level above owncloud folder)
root /var/www;
# set max upload size
client_max_body_size 10G;
fastcgi_buffers 64 4K;
# Disable gzip to avoid the removal of the ETag header
gzip off;
# Uncomment if your server is build with the ngx_pagespeed module
# This module is currently not supported.
#pagespeed off;
# ownCloud blacklist
location ~ ^/owncloud/(?:\.htaccess|data|config|db_structure\.xml|README) {
deny all;
error_page 403 = /owncloud/core/templates/403.php;
}
index index.php;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){
deny all;
}
location /owncloud {
error_page 403 = /owncloud/core/templates/403.php;
error_page 404 = /owncloud/core/templates/404.php;
rewrite ^/owncloud/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/owncloud/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/owncloud/webdav(.*)$ /remote.php/webdav$1 redirect;
rewrite ^(/owncloud/core/doc[^\/]+/)$ $1/index.html;
# The following rules are only needed with webfinger
rewrite ^/owncloud/.well-known/host-meta /public.php?service=host-meta last;
rewrite ^/owncloud/.well-known/host-meta.json /public.php?service=host-meta-json last;
rewrite ^/owncloud/.well-known/carddav /remote.php/carddav/ redirect;
rewrite ^/owncloud/.well-known/caldav /remote.php/caldav/ redirect;
try_files $uri $uri/ index.php;
}
location ~ \.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_param HTTPS on;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location / {
root /var/www/html/;
index index.html;
}
# Optional: set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
# Optional: Don't log access to assets
access_log off;
}
}
I've created a documentation pull request here:
https://github.com/owncloud/documentation/pull/1704
Apologies if you have reviewed this already but there are a few items including multiple Nginx location directives that are absent from the config you posted. I would recommend looking at the configuration notes (link at the bottom of this post) and ensuring that you have Nginx location directives for ownCloud and ownCloud data.
Check the Nginx PHP handler:
Your Nginx configuration should include a handler for PHP5-FPM, put this before the server directive at the top of the Nginx configuration:
upstream php5-fpm-handler {
server unix:/var/run/php5-fpm.sock;
}
Check the Nginx directives:
Examples:
location /owncloud {
rewrite ^ https://$http_host$request_uri? permanent;
}
location ~ ^/owncloud/(data|config|\.ht|db_structure\.xml|README) {
deny all;
}
Check the PHP5-FPM configuration:
Also, please ensure that you PHP5-FPM pool configuration (usually somewhere like /etc/php5/fpm/pool.d/www.conf on Ubuntu) is set to listen on the socket and not a TCP port which should match your handler. The configuration directives for PHP5-FPM socket versus port follow.
Example socket:
listen = /var/run/php5-fpm.sock
Example port (commented out to match the upstream handler):
;listen = 127.0.0.1:9000
Also, if you have not already done so, please take a look at the ownCloud configuration notes for Nginx.
http://doc.owncloud.org/server/5.0/admin_manual/installation/installation_others.html
First let me point out that / isn't working yet /index.php is working, which means that the index statement for some reason isn't working, or that your URI is matching another block.
To be safe rewrite location ~ /owncloud/ to location ^~ /owncloud
Your config needs a lot of rewriting, mind that the default configuration was made for owncloud installed on root directory, yours in a subdirecotry you need to fix few things, like keep in mind that $uri would include /owncloud and /file.ext would hop outside the owncloud folder, so all rewrites that are like
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
need to be fixed because of 2 things,
^/caldav(.*)$ will never happen, uri will always begin with ^/owncloud
/remote.php/... will look outside owncloud
A fix would be something like this:
rewrite ^/owncloud/caldav(.*)$ /owncloud/remote.php/caldav$1 redirect;
Try those for a start and tell me how it goes.

Categories