Nginx地址重定向的實(shí)現(xiàn)
地址重定向
應(yīng)用場(chǎng)景:
- URL 訪問跳轉(zhuǎn),支持開發(fā)設(shè)計(jì)
- 頁(yè)面跳轉(zhuǎn)、兼容性支持、展示效果等
- SEO 優(yōu)化
- 維護(hù)
- 后臺(tái)維護(hù)、流量轉(zhuǎn)發(fā)
- 安全
使用方式:
rewrite ^(.*)$ /pages/maintain.html break;
| 字段 | 作用 |
|---|---|
last | 停止 rewrite 檢測(cè) |
break | 停止 rewrite 檢測(cè) |
redirect | 返回 302 臨時(shí)重定向,地址欄會(huì)顯示跳轉(zhuǎn)后的地址 |
permanent | 返回 301 永久重定向,地址欄會(huì)顯示跳轉(zhuǎn)后的地址(瀏覽器下次直接訪問重定向后的地址) |
root /opt/app/code;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 '{"status": "success"}'
}
location ~ ^/imooc {
#rewrite ^/imooc http://www.imooc.com/ permanent;
#rewrite ^/imooc http://www.imooc.com/ redirect;
}
規(guī)則優(yōu)先級(jí):
- 執(zhí)行 server 塊的 rewrite 指令
- 執(zhí)行 location 匹配
- 執(zhí)行指定的 location 中的 rewrite
常用 301 跳轉(zhuǎn)
# 將 domain.com 重定向到 www.domain.com
server {
listen 80;
server_name domain.com;
rewrite ^/(.*) http://www.domain.com/$1 permanent;
}
server {
listen 80;
server_name www.domain.com;
location / {
root html/brain;
index index.html index.htm;
}
access.log logs/brain.log.main gzip buffer=128k flush=5s;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
跨端適配
Nginx 可以通過內(nèi)置變量 $http_user_agent,獲取到請(qǐng)求客戶端的 userAgent,從而知道用戶處于移動(dòng)端還是 PC 端,進(jìn)而控制重定向到 H5 站還是 PC 站。
location / {
# 當(dāng) userAgent 中檢測(cè)到移動(dòng)端設(shè)備
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
set $mobile_request '1';
}
# 則重定向至
if ($mobile_request = '1') {
rewrite ^.+ http://h5.example.com;
}
}
到此這篇關(guān)于Nginx地址重定向的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx地址重定向內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- nginx 老網(wǎng)站域名重定向到新網(wǎng)站的方法(親測(cè))
- 在Nginx中實(shí)現(xiàn)URL重寫與重定向
- nginx的服務(wù)配置及重定向問題
- nginx重寫和重定向關(guān)系與配置方式
- nginx重定向解決(rewrite or internal redirection cycle)
- Nginx重定向后請(qǐng)求參數(shù)丟失的原因分析及解決方案
- nginx反向代理后無限重定向的問題解決方法
- Nginx中rewrite(地址重定向)的深入剖析
- 利用Nginx實(shí)現(xiàn)URL重定向的簡(jiǎn)單方法
- 配置nginx 重定向到系統(tǒng)維護(hù)頁(yè)面
- nginx中重定向的實(shí)現(xiàn)
相關(guān)文章
Nginx配置網(wǎng)頁(yè)轉(zhuǎn)發(fā)的實(shí)現(xiàn)步驟
本文主要介紹了Nginx配置網(wǎng)頁(yè)轉(zhuǎn)發(fā)的實(shí)現(xiàn)步驟,實(shí)現(xiàn)將云服務(wù)器的80端口轉(zhuǎn)發(fā)到另一臺(tái)服務(wù)器部署的網(wǎng)頁(yè),具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05
通過nginx做mysql的負(fù)載均衡實(shí)現(xiàn)過程
Nginx stream模塊用于DNS和主從MySQL的TCP/UDP代理與負(fù)載均衡,安裝需編譯啟用或通過yum安裝,配置stream塊及upstream實(shí)現(xiàn)轉(zhuǎn)發(fā)和輪詢,測(cè)試后驗(yàn)證端口監(jiān)聽狀態(tài)2025-07-07
阿里云Linux系統(tǒng)Nginx配置多個(gè)域名的方法詳解
本篇文章主要介紹了阿里云Linux系統(tǒng)Nginx配置多個(gè)域名的方法詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Nginx配置location匹配順序詳細(xì)總結(jié)
這篇文章主要介紹了Nginx配置location匹配順序詳解,Nginx是十分輕量級(jí)的HTTP服務(wù)器,Nginx憑借其穩(wěn)定性、低資源消耗、簡(jiǎn)單配置和豐富的功能,從十多年前名不見經(jīng)傳的Web服務(wù)器軟件,發(fā)展到如今能夠跟Apache匹敵的地位,需要的朋友可以參考下2023-08-08

