服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - nginx配置伪静态和适配客户端的方法步骤

nginx配置伪静态和适配客户端的方法步骤

2020-01-08 14:52lingying5 Nginx

这篇文章主要介绍了nginx配置伪静态和适配客户端的方法步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

后端用的thinkphp3.2.3框架,如果您是其他的语言或者,注意适当修改下

?
1
2
3
4
5
6
7
server {
    listen    80;
    server_name www.morgen.club;
 
    #charset koi8-r;
    
    #access_log logs/host.access.log main;

网站根目录

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
location / {
  root  html;
  index index.html index.htm;
  #设置访问设备
  set $source_device 'index';
  #判断为手机客户端或ipad
  if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
    set $source_device 'phone';
  }
  #根目录
  if ($request_uri ~* ^/$){
    rewrite ^(.*)$ /index.php?s=/Home/$source_device/index.html last;
    break;
  }
  #电脑首页
  if ($request_uri ~* ^/Home/index/index.html$) {
    rewrite ^/Home/index/index.html$ /web/ permanent;
    break;
  }
  #电脑首页分页
  if ($request_uri ~* ^/Home/index/index/page/(\d*).html$) {
    rewrite ^/Home/index/index/page/(\d*).html$ /web/pages/$1/ permanent;
    break;
  }
  #电脑首页分类首页
  if ($request_uri ~* ^/Home/index/index/cate/(\d*).html$) {
    rewrite ^/Home/index/index/cate/(\d*).html$ /web/cates/$1/ permanent;
    break;
  }
  #电脑首页分类分页
  if ($request_uri ~* ^/Home/index/index/cate/(\d*)/page/(\d*).html$) {
    rewrite ^/Home/index/index/cate/(\d*)/page/(\d*).html$ /web/cates/$1/pages/$2/ permanent;
    break;
  }
  #电脑首页搜索
  if ($request_uri ~* ^/Home/index/index.html(.*)) {
    rewrite ^/Home/index/index.html(.*) /web/search?$query_string permanent;
    break;
  }
  #电脑书籍页面
  if ($request_uri ~* ^/Home/index/books/book/(\d*).html$) {
    rewrite ^/Home/index/books/book/(\d*).html$ /web/books/$1/ permanent;
    break;
  }
  #电脑章节页面
  if ($request_uri ~* ^/Home/index/contents/books/(\d*)/chapters/(\d*).html$) {
    rewrite ^/Home/index/contents/books/(\d*)/chapters/(\d*).html$ /web/books/$1/chapters/$2/ permanent;
    break;
  }
  #手机首页
  if ($request_uri ~* ^/Home/phone/index.html$) {
    rewrite ^/Home/phone/index.html$ /phone/ last;
    break;
  }
  #手机首页分页
  if ($request_uri ~* ^/Home/phone/index/page/(\d*)(.*)$) {
    rewrite ^/Home/phone/index/page/(\d*).html /phone/pages/$1/ permanent;
    break;
  }
  #手机首页分类首页
  if ($request_uri ~* ^/Home/phone/index/cate/(\d*).html$) {
    rewrite ^/Home/phone/index/cate/(\d*).html$ /phone/cates/$1/ permanent;
    break;
  }
  #手机首页分类分页
  if ($request_uri ~* ^/Home/phone/index/cate/(\d*)/page/(\d*).html$) {
    rewrite ^/Home/phone/index/cate/(\d*)/page/(\d*).html$ /phone/cates/$1/pages/$2/ permanent;
    break;
  }
  #手机首页搜索
  if ($request_uri ~* ^/Home/phone/index.html?(.*)) {
    rewrite ^/Home/phone/index.html(.*) /phone/index.html$1 permanent;
    break;
  }
  #手机书籍页面
  if ($request_uri ~* ^/home/phone/books/book/(\d*).html$) {
    rewrite ^/home/phone/books/book/(\d*).html$ /phone/books/$1/ permanent;
    break;
  }
  #手机章节页面
  if ($request_uri ~* ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$) {
    rewrite ^/home/phone/contents/books/(\d*)/chapters/(\d*).html$ /phone/books/$1/chapters/$2/ permanent;
    break;
  }
  
  if ($request_uri ~* ^/Home/book/(.*)$) {
    rewrite ^(.*)$ /index.php?s=$1 last;
    break;
  }
 
  if (!-e $request_filename) {
    return 404;
  }
}

电脑浏览器端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
location /web {
  root html;
  index index.html index.htm;
  #如果设备为手机或ipad
  if ($http_user_agent ~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
    rewrite ^/web(.*) /phone$1 permanent;
  }
    #电脑首页搜索
    if ($request_uri ~ ^/web/search(.*)$) {
      rewrite ^(.*) /index.php?s=/Home/index/index.html last;
      break;
    }
    #电脑首页
    set $index false;
    if ($request_uri ~* ^/web$) {
      set $index true;
    }
    if ($request_uri ~* ^/web/$) {
      set $index true;
    }
    if ($index = true){
      rewrite ^/web(.*) /index.php?s=/Home/index/index.html last;
      break;
    }
    #电脑首页分页
    set $index_page false;
    if ($request_uri ~* ^/web/pages/(\d*)$) {
      set $index_page true;
    }
    if ($request_uri ~* ^/web/pages/(\d*)/$) {
      set $index_page true;
    }
    if ($index_page = true){
      rewrite ^/web/pages/(\d*)(.*) /index.php?s=/Home/index/index/page/$1.html last;
      break;
    }
    #电脑分类首页
    set $cate false;
    if ($request_uri ~* ^/web/cates/(\d*)$) {
      set $cate true;
    }
    if ($request_uri ~* ^/web/cates/(\d*)/$) {
      set $cate true;
    }
    if ($cate = true){
      rewrite ^/web/cates/(\d*)(.*) /index.php?s=/Home/index/index/cate/$1.html last;
      break;
    }
    #电脑分类分页
    set $cate_page false;
    if ($request_uri ~* ^/web/cates/(\d*)/pages/(\d*)$) {
      set $cate_page true;
    }
    if ($request_uri ~* ^/web/cates/(\d*)/pages/(\d*)/$) {
      set $cate_page true;
    }
    if ($cate_page = true){
      rewrite ^/web/cates/(\d*)/pages/(\d*)(.*) /index.php?s=/Home/index/index/cate/$1/page/$2.html last;
      break;
    }
    
    #电脑书籍页面
    set $book false;
    if ($request_uri ~* ^/web/books/(\d*)$) {
      set $book true;
    }
    if ($request_uri ~* ^/web/books/(\d*)/$) {
      set $book true;
    }
    if ($book = true){
      rewrite ^/web/books/(\d*)(.*) /index.php?s=/Home/index/books/book/$1.html last;
      break;
    }
    #电脑章节页面
    set $chapter false;
    if ($request_uri ~* ^/web/books/(\d*)/chapters/(.*)$) {
      set $chapter true;
    }
    if ($request_uri ~* ^/web/books/(\d*)/chapters/(.*)/$) {
      set $chapter true;
    }
    if ($chapter = true){
      rewrite ^/web/books/(\d*)/chapters/(.*)/ /index.php?s=/Home/index/contents/books/$1/chapters/$2.html last;
      break;
    }
 
    if (!-e $request_filename) {
      return 404;
    }
}

手机和平板端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  location /phone {
    root html;
    index index.html index.htm;
    #如果设备不是手机或ipad
    if ($http_user_agent !~* (mobile|nokia|iphone|ipad|android|samsung|htc|blackberry)) {
      rewrite ^/phone(.*) /web$1 permanent;
    }
      #电脑首页搜索
      if ($request_uri ~ ^/phone/search(.*)$) {
        rewrite ^(.*) /index.php?s=/Home/phone/index.html last;
        break;
      }
      #手机首页
      set $index false;
      if ($request_uri ~* ^/phone$) {
        set $index true;
      }
      if ($request_uri ~* ^/phone/$) {
        set $index true;
      }
      if ($index = true){
        rewrite ^/phone(.*) /index.php?s=/Home/phone/index.html last;
        break;
      }
      #手机首页分页
      set $index_page false;
      if ($request_uri ~* ^/phone/pages/(\d*)$) {
        set $index_page true;
      }
      if ($request_uri ~* ^/phone/pages/(\d*)/$) {
        set $index_page true;
      }
      if ($index_page = true){
        rewrite ^/phone/pages/(\d*)(.*) /index.php?s=/Home/phone/index/page/$1.html last;
        break;
      }
      #手机分类首页
      set $cate false;
      if ($request_uri ~* ^/phone/cates/(\d*)$) {
        set $cate true;
      }
      if ($request_uri ~* ^/phone/cates/(\d*)/$) {
        set $cate true;
      }
      if ($cate = true){
        rewrite ^/phone/cates/(\d*) /index.php?s=/Home/phone/index/cate/$1.html last;
        break;
      }
      #手机分类分页
      set $cate_page false;
      if ($request_uri ~* ^/phone/cates/(\d*)/pages/(\d*)$) {
        set $cate_page true;
      }
      if ($request_uri ~* ^/phone/cates/(\d*)/pages/(\d*)/$) {
        set $cate_page true;
      }
      if ($cate_page = true){
        rewrite ^/phone/cates/(\d*)/pages/(\d*)(.*) /index.php?s=/Home/phone/index/cate/$1/page/$2.html last;
        break;
      }
      #手机书籍页面
      set $book false;
      if ($request_uri ~* ^/phone/books/(\d*)$) {
        set $book true;
      }
      if ($request_uri ~* ^/phone/books/(\d*)/$) {
        set $book true;
      }
      if ($book = true){
        rewrite ^/phone/books/(\d*)(.*) /index.php?s=/Home/phone/books/book/$1.html last;
        break;
      }
      #手机书籍分页页面
      set $book false;
      if ($request_uri ~* ^/phone/books/(\d*)/pages/(\d*)$) {
        set $book true;
      }
      if ($request_uri ~* ^/phone/books/(\d*)/pages/(\d*)/$) {
        set $book true;
      }
      if ($book = true){
        rewrite ^/phone/books/(\d*)/pages/(\d*) /index.php?s=/Home/phone/books/book/$1/page/$2.html last;
        break;
      }
      #手机章节页面
      set $chapter false;
      if ($request_uri ~* ^/phone/books/(\d*)/chapters/(.*)$) {
        set $chapter true;
      }
      if ($request_uri ~* ^/phone/books/(\d*)/chapters/(.*)/$) {
        set $chapter true;
      }
      if ($chapter = true){
        rewrite ^/phone/books/(\d*)/chapters/(.*)/ /index.php?s=/Home/phone/contents/books/$1/chapters/$2.html last;
        break;
      }
 
      if (!-e $request_filename) {
        return 404;
      }
  }
  location ~ .xml(.*) {
    root html/ttt/;
  }
 
  location ~ .txt(.*) {
    root html/ttt/;
  }
  location ~ .gif(.*) {
    root html/ttt/;
  }
  location ~ .jpg(.*) {
    root html/ttt/;
  }
  location ~ .css(.*) {
    root html/ttt/;
  }
  location ~ .js$ {
    root html/ttt/;
  }
  location ~ .php$ {
    root html/ttt;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_buffers 8 256k;
    fastcgi_connect_timeout 300s;
    fastcgi_send_timeout 300s;
    fastcgi_read_timeout 300s;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://segmentfault.com/a/1190000019217031

延伸 · 阅读

精彩推荐
  • Nginx利用nginx和腾讯云免费证书制作https的方法

    利用nginx和腾讯云免费证书制作https的方法

    这篇文章主要介绍了利用nginx和腾讯云免费证书制作https的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    dalaoyang5992019-12-30
  • Nginx如何优化Nginx的处理性能

    如何优化Nginx的处理性能

    Nginx是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性,在连接高并发的情况下,Nginx是Apache服务不错的替代品。其特点是占有内存少,...

    Dockone.io5142020-12-11
  • Nginxnginx rewrite 伪静态配置参数和使用例子

    nginx rewrite 伪静态配置参数和使用例子

    nginx下伪静态配置参数详细说明,使用nginx的朋友,nginx rewrite 伪静态配置参数和使用例子 附正则使用说明 ...

    服务器之家3102019-10-08
  • NginxNginx Rewrite使用场景及代码案例详解

    Nginx Rewrite使用场景及代码案例详解

    这篇文章主要介绍了Nginx Rewrite使用场景及代码案例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可...

    盗哥泡茶去了11862020-09-27
  • NginxNginx location 和 proxy_pass路径配置问题小结

    Nginx location 和 proxy_pass路径配置问题小结

    本文是基于 location 的匹配末尾是否配置 / 和 proxy_pass 末尾是否配置 / ,进行测试,完全还原了整个测试过程,本文给大家介绍Nginx location 基本配置及相关配...

    自由早晚乱余生18742021-09-24
  • Nginx通过Nginx规则重写URL去掉index.php不显示index.php

    通过Nginx规则重写URL去掉index.php不显示index.php

    Nginx不仅占用内存少,并发能力强,而且拓展功能丰富,可以通过安装模板来强化功能,也能通过规则优化,优化服务器并发处理能力,是建站的不二之选...

    Genius日记5872020-10-16
  • Nginxnginx ssl免密码重启教程详解

    nginx ssl免密码重启教程详解

    这篇文章给大家介绍了nginx 如何启动以及nginx ssl 免密码重启 的方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧 ...

    mrr4272019-11-19
  • NginxNginx动静分离实现案例代码解析

    Nginx动静分离实现案例代码解析

    这篇文章主要介绍了Nginx动静分离实现案例代码解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参...

    盗哥泡茶去了3382020-09-27