nginx在https

时间:2016-04-28 10:00:37

标签: nginx https subdomain url-redirection

我有一个VPS Ghost安装,它在nginx上运行。我已经为它创建了一个SSL证书,除了在使用HTTP时所有http://subdomain.example.com总是重定向回我的主https://example.com这一事实之外,一切都运行良好。

但是,如果我访问https://subdomain.example.com,则不会重定向回example.com。我想确保当我的用户访问*.example.com时,他们不会重定向回主域,无论他们是否使用HTTP / S.

这背后的原因是因为我试图在自己的子域上设置ownCloud,并且只能通过example.com/cloud访问它。

我花了很多时间在nginx上配置conf文件,请帮忙!

这是我的两个nginx配置文件 -

对于主域名:

server {
    listen               80;
    server_name          notepad.li;
    ssl_certificate /etc/letsencrypt/live/notepad.li/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/notepad.li/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    return         301 https://$server_name$request_uri;
}

server {                   
    listen               443 ssl;
    server_name          notepad.li;

    root /var/www/ghost/;                                    
    ssl_certificate /etc/letsencrypt/live/notepad.li/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/notepad.li/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;    
    client_max_body_size 200M;

    location ~ /.well-known {
                allow all;
        }

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;                                    
    }     

    location /robots.txt {
        alias /var/www/notepad/robots.txt;
    }
    rewrite ^/cloud$ /cloud/ redirect;
    rewrite ^/cloud/$ /cloud/index.php;
    rewrite ^/cloud/(contacts|calendar|files)$ /cloud/index.php/apps/$1/ redirect;
  rewrite ^(/cloud/core/doc/[^\/]+/)$ $1/index.html;
    location /cloud/ {
    alias /var/www/owncloud/;
    location ~ ^/cloud/(build|tests|config|lib|3rdparty|templates|data|README)/ {
      deny all;
    }

    location ~ ^/cloud/(?:\.|autotest|occ|issue|indie|db_|console) {
      deny all;
    }
  }

  location ~ ^(/cloud)((?:/ocs)?/[^/]+\.php)(/.*)?$ {
    # note: ~ has precendence over a regular location block
    # Accept URLs like:
    # /cloud/index.php/apps/files/
    # /cloud/index.php/apps/files/ajax/scan.php (it's really index.php; see 6fdef379adfdeac86cc2220209bdf4eb9562268d)
    # /cloud/ocs/v1.php/apps/files_sharing/api/v1 (see #240)
    # /cloud/remote.php/webdav/yourfilehere...
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /var/www/owncloud/$2;
    fastcgi_param SCRIPT_NAME $1$2;
    fastcgi_param PATH_INFO $3;
    fastcgi_param MOD_X_ACCEL_REDIRECT_ENABLED on;
    fastcgi_param MOD_X_ACCEL_REDIRECT_PREFIX /owncloud-xaccel;
    fastcgi_read_timeout 630;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    client_max_body_size 1G;
    fastcgi_buffers 64 4K;
  }
  location ^~ /owncloud-xaccel/ {
    # This directory is for MOD_X_ACCEL_REDIRECT_ENABLED. ownCloud sends the full file
    # path on disk as a subdirectory under this virtual path.
    # We must only allow 'internal' redirects within nginx so that the filesystem
    # is not exposed to the world.
    internal;
    alias /;
  }
  location ~ ^/((caldav|carddav|webdav).*)$ {
    # Z-Push doesn't like getting a redirect, and a plain rewrite didn't work either.
    # Properly proxying like this seems to work fine.
    proxy_pass https://127.0.0.1/cloud/remote.php/$1;
  }
  rewrite ^/.well-known/host-meta /cloud/public.php?service=host-meta last;
  rewrite ^/.well-known/host-meta.json /cloud/public.php?service=host-meta-json last;
  rewrite ^/.well-known/carddav /cloud/remote.php/carddav/ redirect;
  rewrite ^/.well-known/caldav /cloud/remote.php/caldav/ redirect;



}

对于子域名:

upstream php-handler {
  server unix:/run/php/php7.0-fpm.sock;
}

server {
  listen 80;
  server_name box.notepad.li;
  # enforce https
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl;
  server_name box.notepad.li;

  ssl_certificate /etc/letsencrypt/live/box.notepad.li/fullchain.crt;
  ssl_certificate_key /etc/letsencrypt/live/box.notepad.li/privkey.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;
  add_header X-Download-Options noopen;
  add_header X-Permitted-Cross-Domain-Policies none;

  # Path to the root of your installation
  root /var/www/owncloud/;
  # 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;

  index index.php;
  error_page 403 /core/templates/403.php;
  error_page 404 /core/templates/404.php;

  rewrite ^/.well-known/carddav /remote.php/dav/ permanent;
  rewrite ^/.well-known/caldav /remote.php/dav/ permanent;

  # The following 2 rules are only needed for the user_webfinger app.
  # Uncomment it if you're planning to use this app.
  #rewrite ^/.well-known/host-meta /public.php?service=host-meta last;
  #rewrite ^/.well-known/host-meta.json /public.php?service=host-meta-json last;

  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }

  location ~ ^/(build|tests|config|lib|3rdparty|templates|data)/ {
    deny all;
  }

  location ~ ^/(?:\.|autotest|occ|issue|indie|db_|console) {
    deny all;
  }

  location / {

    rewrite ^/remote/(.*) /remote.php last;

    rewrite ^(/core/doc/[^\/]+/)$ $1/index.html;

    try_files $uri $uri/ =404;
  }

  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_intercept_errors on;
  }

  # Adding the cache control header for js and css files
  # Make sure it is BELOW the location ~ \.php(?:$|/) { block
  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;
    add_header X-Download-Options noopen;
    add_header X-Permitted-Cross-Domain-Policies 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;
  }
}

关于ownCloud,我复制/粘贴了+修改过的nginx配置代码。我检查了一切,看起来很好。我究竟做错了什么?如果不将http://subdomain.example.com重定向到https://example.com,我为什么不能访问{1}}?

1 个答案:

答案 0 :(得分:0)

一如既往,我忘了三重检查我的nginx核心文件。正如评论中指出的那样,我忘了将include sites-enabled;包含在nginx.conf中,然后在该文件夹中为我的新子域配置创建一个符号链接。

再次感谢!