AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1060096
Accepted
elbarna
elbarna
Asked: 2021-04-13 00:05:22 +0800 CST2021-04-13 00:05:22 +0800 CST 2021-04-13 00:05:22 +0800 CST

带有虚拟站点的 Nginx,所有重写和所有返回都只给出错误“循环重定向错误”

  • 772

我有一个带有 3 个虚拟站点(包括主要真实站点)的 nginx 网络服务器。使用 http 简单工作正常,使用 https(不重定向)工作正常。我想将所有 http 请求重定向到 https,即使对于容器(也称为虚拟主机)也是如此。但是每个客户端(firefox,链接)都会给出 301 错误循环重定向。

这是我的配置

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
    worker_connections 768;
}
http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off; 
    more_clear_headers Server;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_prefer_server_ciphers on;
    ssl_certificate /etc/ssl/certs/http2.mysite.priv.crt;
    ssl_certificate_key /etc/ssl/private/http2.mysite.priv.key;
    access_log /var/log/nginx/access.log ;
    error_log /var/log/nginx/error.log debug;
    gzip on;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

默认站点

server {
    listen 80 default_server;
    listen 443 ssl ;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name http2.mysite.priv;
    location / {
        try_files $uri $uri/ =404;
    }
}

第一个容器/虚拟主机

server {
    listen 80; 
    listen [::]:80 ;
    listen 443 ssl;
    root /var/www/html/virtual1.mysite.priv;
    index index.html index.htm index.nginx-debian.html;
    server_name virtual1.mysite.priv;
    rewrite ^(.*) https://virtual1.mysite.priv$1 permanent;
    location / {
        try_files $uri $uri/ =404;
    }
    
    location /images {
        autoindex on;
    }
}

我也尝试了“return”方法而不是重写,但给了我相同的结果。

return 301 https://$host$request_uri;

我想念什么?

nginx
  • 1 1 个回答
  • 25 Views

1 个回答

  • Voted
  1. Best Answer
    Richard Smith
    2021-04-13T00:54:50+08:002021-04-13T00:54:50+08:00

    使用您的 return 语句创建一个侦听端口 80 的服务器块。从所有其他服务器块中删除listen 80语句。

    例如:

    server {
        listen 80 default_server;
        return 301 https://$host$request_uri;
    }
    
    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve