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 / 问题 / 597302
Accepted
bdesham
bdesham
Asked: 2014-05-21 10:46:33 +0800 CST2014-05-21 10:46:33 +0800 CST 2014-05-21 10:46:33 +0800 CST

使用 nginx 从 URL 中删除尾部斜杠

  • 772

我希望我网站上的以下 URL 是等效的:

/foo/bar
/foo/bar/
/foo/bar/index.html

此外,我希望后两种形式将 HTTP 301 重定向到第一种形式。我只是提供静态页面,它们是按照第三种形式排列的。(换句话说,当用户请求时,/foo/bar他们应该在 接收文件/usr/share/.../foo/bar/index.html)。

我nginx.conf目前包含以下内容:

rewrite ^(.+)/$ $1 permanent;
index index.html;
try_files $uri $uri/index.html =404;

这对. /foo/bar/index.html_ 如何让 nginx 以我描述的方式将 URL 映射到文件?/foo/bar/foo/bar/

编辑:我的完整配置

这是我的全部内容nginx.conf,我的域名替换为“example.com”。

user www-data;
worker_processes 1;
pid /run/nginx.pid;

events {
  worker_connections 768;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  server_tokens off;

  server_names_hash_bucket_size 64;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss application/atom+xml text/javascript image/svg+xml;

  server {
    server_name www.example.com;
    listen 80;
    return 301 $scheme://example.com$request_uri;
  }

  server {
    server_name example.com 123.45.67.89 localhost;
    listen 80 default_server;

    # Redirect /foobar/ to /foobar
    rewrite ^(.+)/$ $1 permanent;

    root /usr/share/nginx/www/example.com;
    index index.html;
    try_files $uri $uri/index.html =404;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    location = /50x.html {
      root /usr/share/nginx/html;
    }
  }
}
nginx
  • 5 5 个回答
  • 59554 Views

5 个回答

  • Voted
  1. Francisco Costa
    2015-05-12T09:54:42+08:002015-05-12T09:54:42+08:00

    server在你的块上有这个正则表达式:

    rewrite ^/(.*)/$ /$1 permanent;
    

    会将所有尾部斜杠 URL 重定向到相应的非尾部斜杠。

    • 27
  2. Eldar Agalarov
    2018-01-04T20:43:12+08:002018-01-04T20:43:12+08:00

    永远不要使用重写:

      location ~ (?<no_slash>.*)/$ {
           return 301 $scheme://$host$no_slash;
      }
    
    • 15
  3. Best Answer
    bdesham
    2014-05-23T07:30:40+08:002014-05-23T07:30:40+08:00

    server通过将其用作配置中的最后一个块,我能够获得所需的行为:

    server {
      server_name example.com 123.45.67.89 localhost;
      listen 80 default_server;
    
      # Redirect /foobar/ and /foobar/index.html to /foobar
      rewrite ^(.+)/+$ $1 permanent;
      rewrite ^(.+)/index.html$ $1 permanent;
    
      root /usr/share/nginx/www/example.com;
      index index.html;
      try_files $uri $uri/index.html =404;
    
      error_page 404 /404.html;
      error_page 500 502 503 504 /50x.html;
    
      location = /50x.html {
        root /usr/share/nginx/html;
      }
    }
    
    • 6
  4. KuN
    2021-02-21T10:56:59+08:002021-02-21T10:56:59+08:00
        location ~ ^(.+)/$ {
          return 301 $scheme://$host$1;
        }
    

    使用它来托管来自 Next.js (v9&v10) 静态站点生成的站点,这些站点host/url在host/url/触发 Nginx 404 时工作。

    • 2
  5. Ashish Amre
    2018-12-04T20:54:32+08:002018-12-04T20:54:32+08:00
    if ($request_uri ~ (.*?\/)(\/+)$ ) {
    return 301 $scheme://$host$1;
    }
    

    此规则将处理任意数量的尾部斜杠并保留 URL。它还将处理基本 URL 尾部斜杠

    • 0

相关问题

  • 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