【问题标题】:ssl for subdirectory within nginx server configurationssl 用于 nginx 服务器配置中的子目录
【发布时间】:2012-01-26 17:04:12
【问题描述】:

我有一个运行 ssl 的 nginx 服务器。 目前我为所有目录启用了 https。 如何只为www.example.com/shop/* 目录启用 ssl 并为其他目录禁用??

这是我的配置文件:

    # Redirect everything to the main site.
server {
  server_name *.example.com;
  listen 80;
  ssl on;
  ssl_certificate /opt/nginx/conf/server.crt;
  ssl_certificate_key /opt/nginx/conf/server.key; 
  keepalive_timeout    70;

  access_log  /home/example/nginx_logs/access.log ;
  error_log  /home/example/nginx_logs/error.log ;

  root /home/example/public_html/example.com;   
  location ~ \.php$ {
      try_files $uri $uri/ /index.php?q=$uri&$args;         
      root          /home/example/public_html/example.com/;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      include        /opt/nginx/conf/fastcgi_params;
      #fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script;
      fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
      fastcgi_param  SCRIPT_FILENAME  /home/example/public_html/example.com$fastcgi_script_name;
      index  index.php index.html index.htm;   
  }

    if ($http_host != "example.com") {
        rewrite ^ http://example.com$request_uri permanent;
    }

    include global/restrictions.conf;

    # Additional rules go here.

    #Only include one of the files below.
    include global/wordpress.conf;
#   include global/wordpress-ms-subdir.conf;
#   include global/wordpress-ms-subdomain.conf;
}

坦克, D

【问题讨论】:

  • 没办法。 ssl 是仅位于 httpserver 下的指令(如文档所述)

标签: ssl nginx


【解决方案1】:

在 Nginx 中很容易实现。它涉及两个步骤。

  1. 仅当访问 yourdomain.com/shop 时才会使用端口 443。所有其他请求将被重定向到端口 80 (HTTP)
  2. 端口 80 将检查 yourdomain.com/shop。如果找到,它将被重定向到端口 443 (HTTPS)。

这里是如何完成它的快速概述......

server {
  listen 443;
  server_name yourdomain.com;

  # directives for SSL certificates

  # root, index, error_log, access_log directives

  location /shop {
    # directives to handle what's inside /shop, for example
    # try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {
    # directives to handle PHP files
  }

  # leave everything else to port 80
  location / {
    rewrite ^ http://$host$request_uri permanent;
  }
}

server {
  listen 80;
  server_name yourdomain.com;

  # root, index, error_log, access_log directives

  # redirect yourdomain.com/shop to port 443
  # Please put this before location / block as
  # nginx stops after seeing the first match
  location /shop {
    rewrite ^ https://$host$request_uri permanent;
  }

  location / {
    # directives to handle what's inside /, for example
    # try_files $uri $uri/ /index.php;
  }

  location ~ \.php$ {
    # directives to handle PHP files
  }

}

【讨论】:

  • 感谢@atmosx 纠正错字和选项命名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 2016-08-28
  • 2019-09-19
  • 1970-01-01
  • 1970-01-01
  • 2017-05-12
  • 1970-01-01
相关资源
最近更新 更多