NGINX - Probleme mit Site-Konfiguration (Owncloud + fhem)

Begonnen von Klouse, 03 Dezember 2015, 09:03:20

Vorheriges Thema - Nächstes Thema

Klouse

Hallo,

ich habe Debian Jessie mit NGINX im Einsatz,
auf diesem ist fhem als auch owncloud aktiv.

jedoch will meine Config einfach nicht richtig funktionieren.

Was ich erreichen will:
Aufruf von FHEM: https://<servername>/fhem
Aufruf von OwnCloud: https://<servername>/owncloud

Bisher habe ich es nur mit einer Trennung der Ports geschafft,
am liebsten hätte ich jedoch einfach eine Lösung per Location.

Hier meine beiden "nicht-funktionierenden" Config-Files, ändere ich bei einem der beiden das listen z.B. auf 450, läuft es einwandfrei.

Config-FHEM:
server {
listen 443 ssl;
server_name test;

ssl_certificate /etc/ssl/nginx/test.cert;
ssl_certificate_key /etc/ssl/nginx/test.key;

access_log /var/log/nginx/fhem.access.log;
error_log /var/log/nginx/fhem.error.log;

location /fhem {
proxy_pass http://localhost:8083/fhem;
proxy_redirect off;
proxy_buffering off;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}


Config-OwnCloud:
upstream php-handler {
server 127.0.0.1:9000;
#server unix:/var/run/php5-fpm.sock;
}

server {
listen 80;
server_name test;
# enforce https
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;
server_name test;

ssl_certificate /etc/ssl/nginx/test.cert;
ssl_certificate_key /etc/ssl/nginx/test.key;

# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";

# Path to the root of your installation
root /opt/;
# set max upload size
client_max_body_size 16G;
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;


index index.php;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.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/ {
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;

# 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;

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 php-handler;
fastcgi_intercept_errors on;
}

# Adding the cache control header for js and css files
location ~* \.(?:css|js)$ {
add_header Cache-Control "public, max-age=7200";
# 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;
# Optional: Don't log access to assets
access_log off;
}

# Optional: Don't log access to other assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|swf)$ {
access_log off;
}
}


Ich habe auch bereits versucht nachfolgenden Block einfach in das owncloud-file zu migrieren,
das würde auch klappen, jedoch funktioniert die darstellung von fhem dann nicht mehr richtig,
nur noch schwarzer Text auf weißem Hintergrund.

location /fhem {
proxy_pass http://localhost:8083/fhem;
proxy_redirect off;
proxy_buffering off;
auth_basic "Restricted Access";
auth_basic_user_file /etc/nginx/.htpasswd;


Vielleicht ist ein nginx-Meister unter euch der mir hier weiter helfen kann,
danke! :)

LG

DecaTec

Zitat von: Klouse am 03 Dezember 2015, 09:03:20
Ich habe auch bereits versucht nachfolgenden Block einfach in das owncloud-file zu migrieren,
das würde auch klappen, jedoch funktioniert die darstellung von fhem dann nicht mehr richtig,
nur noch schwarzer Text auf weißem Hintergrund.

Macht auch Sinn, ein config-File für einen Server(-Block).
Hast du den FHEM-Block über den ownCloud-Block gesetzt?

Ich habe fast genau die gleiche Konfiguration (ownCloud und FHEM über nginx), habe jedoch im Moment keinen Zugriff auf die config-Files.
Kann dir heute abend aber gern mal meine Config posten, mit der das ganze problemlos klappt.

Klouse

Hey DecaTec,

ich hatte es an mehreren Positionen, ist jetzt aber schon ein paar Wochen her.

Vielen Dank, ich bin sehr gespannt auf deine Konfig,
dann spare ich mir weitere schlaflose Nächte. *gg*

LG
Klaus

DecaTec

So, hier mal meine Config (nginx):


upstream php-handler {
  server unix:/var/run/php5-fpm.sock;
  }

server {
  listen 80;
  server_name <URL_or_IP>;
  # enforce https
  return 301 https://$server_name$request_uri;
  }

server {
  listen 443 ssl;
  server_name <URL_or_IP>;

  ssl_certificate /etc/ssl/nginx/test.cert;
  ssl_certificate_key /etc/ssl/nginx/test.key;

  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;

  ssl_dhparam /etc/nginx/ssl/dhparams.pem;
  ssl_ciphers '<ciphers>'
  ssl_prefer_server_ciphers on;

  root /var/www/;

  gzip on;
 
  index index.php index.html;

  location / {
     deny all;
  }

  # General PHP handler 
  location ~ \.php$ {
   include fastcgi_params;
   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   fastcgi_param HTTPS on;
   fastcgi_pass php-handler;
   }
   
   #
   # FHEM
   #
   location /fhem {
proxy_pass http://<IP>:<Port>
        proxy_redirect off;
proxy_buffering off;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
   }   
   
   #
   # ownCloud
   #   
   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;
   
   location /owncloud {
    client_max_body_size 513M;
    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;
   
    error_page 403 /owncloud/core/templates/403.php;
    error_page 404 /owncloud/core/templates/404.php;

    # The following 2 rules are only needed with webfinger
    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/core/doc/[^\/]+/)$ /owncloud/$1/index.html;

    try_files $uri $uri/ /owncloud/index.php;

     # ownCloud specific PHP handler
     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_param modHeadersAvailable true; #Avoid sending the security headers twice
      fastcgi_pass php-handler;
      fastcgi_read_timeout 300;
      fastcgi_param PHP_VALUE "open_basedir=/var/www:/tmp/:/var/ownclouddata:/dev/urandom";
     }
   } 
   
   location = /owncloud/robots.txt {
    allow all;
    log_not_found off;
    access_log off;
   }

   location ~ ^/owncloud/(?:\.htaccess|data|config|db_structure\.xml|README){
    deny all;
   }

   # Optional: set long EXPIRES header on static assets
   location ~* ^/owncloud(/.+\.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf))$ {
    expires 30d;
    # Optional: Don't log access to assets
    access_log off;
   }
}


Vielleicht noch ein paar Erklärungen:


  • Die "add_header" Sachen werden in der ownCloud-Doku empfohlen, für FHEM schaden sie nicht, deshalb stehen diese auch direkt im server-Block.
  • ssl_dhparam, ssl_ciphers und ssl_prefer_server_ciphers: optional, Diffie-Hellman für TLS; dient dazu, die Sicherheit weiter zu erhöhen.
  • Aufteilung "genereller" und ownCloud-spezifischer PHP-Handler: PHP habe ich relativ sicher konfiguriert (Stichwort "open_basedir" in php.ini). Daher werden im ownCloud-spezifischen Handler nochmals einige Parameter explizit gesetzt.
  • Die ganzen rewrites bzgl. ownCloud stammen hauptsächlich auch aus der ownCloud-Doku, mussten allerdings angepasst werden, damit ownCloud in einem Unterverzeichnis des Webroots liegen kann.
Falls du noch fragen hast, nur zu (gern auch per PM), die Sache ist relativ komplex und ich habe auch einige Zeit gebraucht, bis alles so lief, wie ich es haben wollte.
Habe deshalb auch einen Artikel darüber geschrieben: ownCloud auf Ubuntu Server mit nginx, MariaDB und PHP. Kannst da ja vielleicht auch mal reinschauen, sollte alles ausführlich erklärt sein. Zielt zwar nur auf ownCloud ab, der Schritt, dort FHEM einzubinden ist allerdings nur noch ein kleiner.

Klouse

So,

ich kam nun endlich dazu - es funktioniert wunderbar. :)

nochmals vielen Dank DecaTec!

MfG
Klaus