Nginx 服务器重定向被忽略

Nginx 服务器重定向被忽略

我肯定在某个地方把它塞住了,但不知道在哪里。Nginx 似乎忽略了以下重定向。

它当前位于其自己的 conf 文件中,但我已将其放在其他文件中。

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

这是有效的 SSL 配置:

#HTTPS
server {
    listen      10.10.15.30:443 ssl;
    ssl         on;
    server_name domain.com;
    access_log  /var/log/nginx/ssl-access.log;
    error_log   /var/log/nginx/ssl-error.log;
    ssl_certificate     /etc/nginx/ssl/domain.com.crt;
    ssl_certificate_key /etc/nginx/ssl/domain.com.key;
    ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    keepalive_timeout    60;
    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

location / {
    proxy_set_header Host $host;
    # So the original HTTP Host header is preserved
    proxy_set_header X-Real-IP          $remote_addr;
    # The IP address of the client (which might be a proxy itself)
    proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto  $scheme;
    add_header       Front-End-Https    on;
    proxy_pass http://backend;
}

}

以下是后端服务器的配置,其功能如下:

upstream backend {
  server 10.10.15.21 max_fails=2 fail_timeout=15s;
  server 10.10.15.22 max_fails=2 fail_timeout=15s;
}

#HTTP
server {
  listen 10.10.15.30:80 default_server;
  #listen 80;
  server_name _;
  location / {
      proxy_set_header Host $host;
      # So the original HTTP Host header is preserved
      proxy_set_header X-Real-IP $remote_addr;
      # The IP address of the client (which might be a proxy itself)
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://backend;
  }
}


server {
    listen 10.10.3.10:61709;
    location /nginx_status {
    stub_status on;
    access_log off;
    allow 127.0.0.1;
    deny all;
    }
}

后端服务器处理多个站点。

答案1

您错过了^重写。

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

答案2

因此,我进行了以下更改以使其正常工作:

mv /etc/nginx/conf.d/domain.com.ssl.conf /etc/nginx/conf.d/domain.com.sslconf

然后我编辑了 /etc/nginx/conf.d/domain.com.sslconf 并将以下内容添加到顶部:

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

然后我编辑 /etc/nginx/conf.d/backends.conf 并在主服务器下方添加以下内容:

include /etc/nginx/conf.d/*.sslconf;

这与配置的优先级/顺序加载有关。

如果这不清楚,我可以发布完整的配置并突出显示更改。

答案3

某种循环?如果在其中输入“break”,它会起作用吗?

根据我的理解,重写规则上只能有一个标志,因此请先尝试进行测试:

server {
  server_name      domain.com;
  listen 80;
  rewrite          ^    https://$server_name$request_uri? break;
}

由于您的重写应该始终匹配,因此您应该能够像这样获取永久重定向状态:

server {
  server_name      domain.com;
  listen 80;
  rewrite          ^    https://$server_name$request_uri? break;
  return   301;
}

或者,尝试“break”指令。

相关内容