【问题标题】:Nginx leads to endless redirect loop after adding ssl certificatesNginx 添加 ssl 证书后导致无限重定向循环
【发布时间】:2016-02-02 18:20:35
【问题描述】:

我已将 ssl 证书添加到我的网站,但它导致了重定向循环。 它以前工作得很好。 我试过了:

以及我找到的其他解决方案, 我怀疑它可能与文件权限有关,但我没有找到任何东西。 我也尝试将我的 crt 和密钥文件放在 /etc/ssl 中。

这是我的 default.website 文件:

server {
    listen      80;
    server_name www.example.com example.com;
    rewrite     ^   https://$server_name$request_uri? permanent;
    }


server {
    listen 443;

    ssl on;
    ssl_certificate /etc/nginx/ssl/ssl-bundle.crt;
    ssl_certificate_key /etc/nginx/ssl/private.key;

    #listen       80 default_server;
    #listen       [::]:80 default_server;
    server_name  example;
    access_log /var/log/nginx/whcms.log;

    location / {
            root   /usr/share/nginx/html/example.com;
            index index.php index.html;
            try_files $uri $uri/ /index.php?$args;
    }

    location /ngx_status_2462 {
      stub_status on;
      access_log   off;
      allow all;
    }

    #location / {
    #        index index.php index.html;
    #        try_files $uri $uri/ /index.php?$args;
    #}


    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/example.com$fastcgi_script_name;
            fastcgi_param CCODE $geoip_country_code;
            include fastcgi_params;
    }

    error_page  404              /404.html;

    location = /404.html {
        root   /usr/share/nginx/html;
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

这是我的 nginx.conf 文件:

user  nginx;
worker_processes  12;
worker_rlimit_nofile 100000;
error_log   /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  100000;
    use epoll;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    keepalive_requests 0;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$host" "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

   set_real_ip_from   0.0.0.0/0;
   real_ip_header     CF-Connecting-IP;

    geoip_country   /usr/share/GeoIP/GeoIP.dat;


    access_log  /var/log/nginx/nginx-access.log  main  buffer=256k;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    server_tokens   off;

## Caching files
...

## Size Limits
  ...

## Timeouts
  ...

## Compression
  ...

  #include /etc/nginx/sites-enabled/*;

    # Load config files from the /etc/nginx/conf.d directory
    include /etc/nginx/conf.d/*.conf;
    index  index.php index.html index.htm;
    include /etc/nginx/websites/*.website;


}

这是错误日志(添加调试后):

2015/11/03 09:22:09 [notice] 30330#0: *1 "^" matches "/", client: <ip_addr>, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
2015/11/03 09:22:09 [notice] 30330#0: *1 rewritten redirect: "https://example.com/", client: <ip_addr>, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
2015/11/03 09:22:10 [notice] 30330#0: *2 "^" matches "/", client: <ip_addr>, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
2015/11/03 09:22:10 [notice] 30330#0: *2 rewritten redirect: "https://example.com/", client: <ip_addr>, server: example.com, request: "GET / HTTP/1.1", host: "example.com"
2015/11/03 09:22:10 [notice] 30330#0: *1 "^" matches "/", client: <ip_addr>, server: example.com, request: "GET / HTTP/1.1", host: "example.com"

服务器ip地址在哪里

我使用 cloudflare 作为我的 dns 提供商

【问题讨论】:

    标签: ssl nginx


    【解决方案1】:

    我首先看到的是你443服务器中的server_name是错误的。

    并查看您的 443 听,您应该是:

    listen 443 ssl;

    您使用的是 AWS ELB 吗??

    【讨论】:

    • 不,我使用的是我自己的服务器,它运行 centos 7.1,我尝试了不同的 server_names,包括 example.com www.example.com
    • 将您的收听 443 更改为 listen 443 ssl;
    猜你喜欢
    • 2011-06-04
    • 2015-04-19
    • 2012-03-05
    • 2015-11-28
    • 2015-01-08
    • 2013-04-07
    • 2019-08-31
    • 2015-02-26
    • 1970-01-01
    相关资源
    最近更新 更多