nginx篇最初级用法之地址重写

nginx服务器的地址重写,主要用到的配置参数是rewrite

  • rewrite regex replacement flag
  • rewrite 旧地址 新地址 [选项]

支持的选项有:

  • last 不再读其他rewrite
  • break 不再读其他语句,结束请求
  • redirect 临时重定向
  • permament 永久重定向
  • 在nginx网页目录中添加两个页面进行测试
  • [[email protected] html]# echo "new page" > new.html
  • [[email protected] html]# echo "old page" > old.html
  • [[email protected] html]# curl http://localhost/old.html
  • old page
  • [[email protected] html]# curl http://localhost/new.html
  • new page

将old.html重定向到new.html

重载nginx配置文件

[[email protected] sbin]# ./nginx -s reload

访问测试

可以看到重定向好处在不改变地址栏的情况下将也没重定向到其他页面,

比如说跳转到:对不起不访问的页面不存在....

当然也可以在rewrite后添加redirect 属性让地址栏也一起跳转.

rewrite /old.html /new.html redirect;

当然也可以多种用法例如将所有A网站的请求转发至B网站

rewrite ^/ http://www.baidu.com/;

也可以讲A网站下请求的所有子页面请求转向B网站访问同样的页面地址

rewrite ^/(.*)$ http://www.baidu.com/$1;

原文地址:https://www.cnblogs.com/lqinghua/p/11620299.html

时间: 2024-08-01 20:57:11

nginx篇最初级用法之地址重写的相关文章

nginx篇最初级用法之三种虚拟主机基于域名\基于端口\基于IP地址端口的虚拟主机

在nginx中虚拟主机的类型与apache一样也有三种 1.基于域名的虚拟主机 2.基于端口的虚拟主机 3.基于IP地址端口的虚拟主机 在nginx配置文件中每一个server为一个虚拟主机如果需要多个虚拟主机只需要添加server即可例如 server{ listen 80; server_name www.lqinghua.com   //基于域名的虚拟主机 location / { root def; index index.html; } } server{ listen 8080;  

Nginx篇--最初级用法web

最近很久都没有写博客了,一来主要是时间不够每天回到家都接近晚上11点了,但是以后每天还是保证一篇随笔.好用来整理总结自己的知识. web服务器很有多例如:Apache nginx tengine lighttpd Tomcat Websphere Jboss IIS等,今天总结是nginx nginx这个国内应用得非常广泛,例如淘宝不仅仅用nginx还用出了经验从而还在nginx之上推出了Tengine. 一.安装nginx 前提:所有机器已经配置好yum源. 先设置两台web服务器 使用eth

nginx篇最初级用法之nginx升级

在不破坏nginx应用程序和配置文件下时,升级nginx. 下载新版本的nginx对其进行编译 使用nginx -V参数获得当前版本和配置参数 nginx version: nginx/1.10.3built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017TLS SNI support enabledconfigure arguments: --prefix=/us

nginx篇最初级用法之访问认证

1打开conf下的配置文件 在server 之下 location 之上加入 auth_basic "Input Password:";    弹出的提示信息 auth_basic_user_file "/usr/local/nginx/pass";    认证的密码文件 2.使用htpasswd生成相应的密码文件(需要安装httpd-tools包). htpasswd -c /usr/local/nginx/pass lqinghua 输入两次密码即可 原文地址:

nginx篇最初级用法之SSL虚拟主机

注意:在源码安装nginx时必须要使用--with-http_ssl_module参数启动加密模块. openssl genrsa > cert.key  //使用openssl自己签发私钥 openssl req -new -x509 -key cert.key > cert.pem //签发证书 修改nginx配置文件,设置加密网站的虚拟主机 server { listen 443 ssl; server_name          www.baidu.com; ssl_certifica

nginx篇最初级用法之lnmp环境搭建

这里m使用mariadb 需要下列软件列表: nginx mariadb 数据库客户端软件   mariadb-server   数据库服务器软件   mariadb-devel  其他客户端软件的依赖包 php php解释器  php-fpm php进程管理器服务 php-mysql php的数据库拓展包 安装前提:yum源配置好,使用光盘自带的yum仓库 yum install -y mariadb mariadb-server mariadb-devel php php-fpm php-m

Nginx 的编译安装和URL地址重写

本文转自:http://www.178linux.com/14119#rd?sukey=ecafc0a7cc4a741b573a095a3eb78af6b4c9116b74d0bbc9844d8fc5e8b50b3fc807541ae53fd06c67ac4f4adaae6981 在此只是做个笔记给自己看的. Nginx专题: 从编译安装到URL重写 前言 环境介绍 Nginx介绍 编译安装Nginx 配置文件解释 main和event{}的配置 http{}的基本配置 配置Nginx 搭建一个

命令分析nginx访问日志的用法

awk分析日志常用高级使用命令方法 分析访问日志(Nginx为例) 日志格式: '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"' 统计访问IP次数: # awk '{a[$1]++}END{for(i

Apache Nginx URL 地址 重写

URL重写这东西在工作中用了很多次了,但每次都忘记了要记得把知道的积累下来. 哎,要么认为没必要,要么就是没时间?! 一.Apache 篇 官方地址:http://man.chinaunix.net/newsoft/ApacheManual/mod/mod_rewrite.html 1.htaccess基本语法介绍 服务器有配置文件不可能由我们来改,所以大多情况下要在网站的根目录下建一个.htaccess文件. #设置重写的根目录RewriteBase /#开启重写引擎RewriteEngine