Nginx rewrite URL examples with and without redirect address
原文地址: http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-address#.VY9nfJeqqko
Nginx can handle the rewrite parameter differently, depending on the destination syntax.
Here are some examples how to define redirects and URL rewrites in nginx.
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ http://websrv1.example.com/mypage redirect;
}
}
This will result in forwarding the browser to http://websrv1.example.com/mypage. The redirect address will be shown in the address bar.
Let's try this without a redirect or permanent option but with break or last:
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ http://websrv1.example.com/mypage last;
}
}
Although the rewrite option is now set to last, the browser will still follow the URL and changes the URL in the address bar.
The reason for this is the http:// which is interpreted as external redirect.
So if you want to keep your domain and simply want to rewrite the URL (like in Apache with mod_rewrite), you must use a relative path:
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
rewrite ^/$ /mypage last;
}
}
This will load the website for www.example.com from the subfolder /mypage within the document root (/var/www/www.example.com).
But what if the destination website is loaded from somewhere else, for example from a Tomcat server in the background?
The following configuration covers this:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
server_name www.example.com;
root /var/www/www.example.com;
location / {
include proxy-settings.conf;
proxy_pass http://tomcat;
rewrite ^/$ /mypage last;
}
}
First everything (location /) is passed to tomcat (the defined upstream server). Then the redirect for the root path (/) is happening and is relative to the path.
This results in keeping the browser's address URL at www.example.com but loads the website from 127.0.0.1:8080/mypage.
Nginx rewrite URL examples with and without redirect address的更多相关文章
- nginx的url重写[rewrite规则和参考]
本日志内容来自互联网和平日使用经验,整理一下方便日后参考. Nginx Rewrite 相关指令有 if.rewrite.set.return 等. if 的语法 应用于 server 和 locat ...
- nginx rewrite 实现URL跳转
最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...
- Nginx – rewrite 配置 URL重写及301跳转原理图
Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...
- (Nginx) URL REWRITE
URL重写的基础介绍 把URI地址用作参数传递:URL REWRITE 最简单的是基于各种WEB服务器中的URL重写转向(Rewrite)模块的URL转换: 这样几乎可以不修改程序的实现将 news. ...
- 再谈Nginx Rewrite, 中文URL和其它
上次谈到过Nginx和中文URL的问题,这几天又加深了认识. 多分享几个关于Nginx Rewrite的经验. Nginx匹配指定中文URL的方法:rewrite "(*UTF8)^x{66 ...
- thinkphp nginx php-fpm url rewrite 导致 404 错误
## thinkphp nginx php-fpm url rewrite 导致 404 错误 之前thinkphp的系统部署在apache上,考虑到在并发性能nginx比apache强悍得多,所以在 ...
- nginx——rewrite模块
1.什么是Nginx的Rewrite规则? Rewrite主要的功能就是实现URL的重写,Nginx的Rewrite规则采用PCRE(Perl Compatible Regular Expressio ...
- Nginx Rewrite规则初探(转)
Nginx rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...
- Nginx Rewrite规则记录
Rewrite 是一种服务器的重写脉冲技术,它可以使得服务器可以支持 URL 重写,是一种最新流行的服务器技术.它还可以实现限制特定IP访问网站的功能.很多情况下,某个 IP 的访问很容易造成 CPU ...
随机推荐
- 【Android开发】之Fragment重要函数讲解
上一节我们讲到了Fragment的生命周期(都是基于android.support.v4.app包下的Fragment),学习之后相信大家对它的生命周期有了很深入的了解了,如果还有不懂得,可以再看一下 ...
- Oracle优化技巧
现观察线上系统运行发现,线上某些业务查询存在等待时间长问题,后核查发现,部分问题出现在对数据库操作上Cost大部分时间,后根据网上各位前辈提供的优化技巧解决大部分问题,现写下本篇文章,一来巩固加深自己 ...
- ThinkPHP中的统计查询方法
• count() 表示查询表中总的记录数 • max() 表示查询某个字段的最大值 • min() 表示查询某个字段的最小值 • avg() 表示查询某个字段的平均值 • sum() 表示求出某个字 ...
- 更换网页tab标题图标
在首页HTML文件中,加入link命令,<link>是放在<head>与</head>之间 例如下面这样: <HEAD><link rel = & ...
- node 使用笔记
1 安装 buffertools 因为使用mjpeg-proxy的关系,需要编译buffertools中的C++代码文件,怎奈何一直出错. make: Entering directory `/srv ...
- CF 554A 字符串水题
给出一个字符串,问再加入一个字母,最多能形成多少种字符串 inputaoutput51inputhioutput76 # include <iostream> # include < ...
- NET定时任务组件Hangfire
开源的.NET定时任务组件Hangfire解析 项目慢慢就要开工了,很多园友都在问这个事情,看来大伙对这事很上心啊,事情需要一步步的来,尽量写出一个我们都满意的项目.以前每次在博客前面都会扯淡一下,不 ...
- Linux性能优化之CPU优化(一)
前言 何为性能优化?个人认为,性能优化是为了提高应用程序或系统能力为目的.那么如何才能实现对应用程序的性能调优呢?这里很设计到很多的内容,包括Linux内核.CPU架构以及Linux内核对资源的分配以 ...
- ssh端口转发(之kettle ssh方式连接数据库)
ssh参数解释 格式 ssh [user@]host [command] 选项: -1:强制使用ssh协议版本1: -2:强制使用ssh协议版本2: -4:强制使用IPv4地址: -6:强制使用IP ...
- 括弧匹配检验(check.cpp)
[问题描述] 假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[([ ][ ])]等为正确的匹配,[( ])或([ ]( )或 ( ( ) ) )均为错 ...