linux系统rewrite重定向及搭建discuz

rewrite重定向

作业需求

背景:现在我有一个网站,www.linux.com

www.linux.com访问主页面

friend.linux.com访问交友页面

blog.linux.com访问博客页面

download.linux.com访问博客页面

在nginx上部署三套代码

使用rewrite和return两种方式完成以下需求

1、通过www.linux.com/download访问到下载页面

2、通过www.linux.com/friends访问到交友页面

3、通过www.linux.com/blog访问到博客页面

做题思路

环境准备

首先准备一台虚拟机,关闭防火墙,下载nginx服务

然后编辑nginx的配置文件,根据需求创建站点目录,

检查配置文件,启动nginx,检测端口和进程,创建出相应的站点目录,域名解析

操作过程

服务器名称 外网IP 内网IP 安装服务
web01 10.0.0.7 172.16.1.7 nginx

web01配置

# 安装nginx服务
[root@web01 ~]# yum install -y nginx
# 修改nginx主配置文件,打开rewrite调试规则日志
[root@web01 ~]# cd /etc/nginx/
[root@web01 nginx]# vim nginx.conf
http {
      rewrite_log on;

# 编辑配置文件
[root@web01 nginx]# vim conf.d/www.linux.com.conf 

server {
        listen 80;
        server_name www.linux.com;
        root /linux;
        index index.html;

        location ~ /blog {
            rewrite ^(.*)$ http://blog.linux.com redirect;
        #   return 301 http://blog.linux.com;
        }

        location ~ /friend {
            rewrite ^(.*)$ http://friend.linux.com redirect;
            #return 301 http://friend.linux.com;
        }

        location ~ /download {
            rewrite ^(.*)$ http://download.linux.com redirect;
            #return 301 http://download.linux.com;
        }
}


server {
        listen 80;
        server_name blog.linux.com;
        root /blog;
        index index.html;
}
server {
        listen 80;
        server_name friend.linux.com;
        root /friend;
        index index.html;
}
server {
        listen 80;
        server_name download.linux.com;
        root /download;
        index index.html;
}

# 检查配置文件语法
[root@web01 nginx]# nginx -t
# 启动nginx并加入开机自启
[root@web01 nginx]# systemctl start nginx && systemctl enable nginx

# 检查nginx的端口和进程
[root@web01 nginx]# netstat -lntup|grep nginx && ps -ef |grep [n]ginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      10296/nginx: master 
root      10296      1  0 Jun01 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     12520  10296  0 18:08 ?        00:00:00 nginx: worker process

# 域名解析
#浏览器访问

跳转blog

跳转download

跳转friend

注释掉rewrite,打开return

[root@web01 nginx]# vim conf.d/www.linux.com.conf 

server {
        listen 80;
        server_name www.linux.com;
        root /linux;
        index index.html;
        
        location ~ /blog {
          #  rewrite ^(.*)$ http://blog.linux.com redirect;
           return 301 http://blog.linux.com;
        }
        
        location ~ /friend {
           # rewrite ^(.*)$ http://friend.linux.com redirect;
            return 301 http://friend.linux.com;
        }
        
        location ~ /download {
           # rewrite ^(.*)$ http://download.linux.com redirect;
            return 301 http://download.linux.com;
        }
}


server {
        listen 80;
        server_name blog.linux.com;
        root /blog;
        index index.html;
}
server {
        listen 80;
        server_name friend.linux.com;
        root /friend;
        index index.html;
}
server {
        listen 80;
        server_name download.linux.com;
        root /download;
        index index.html;
}

# 检查语法,重新加载nginx
[root@web01 nginx]# nginx -t
nginx: the configuration file /app/nginx-1.16.1_new1/conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.16.1_new1/conf/nginx.conf test is successful

[root@web01 nginx]# systemctl reload nginx
# 浏览器访问

跳转download

跳转blog

跳转friend

部署discuz

环境准备

服务器名称 外网IP 内网IP 安装服务
web01 10.0.0.7 172.16.1.7 nginx,php-fpm,discuz
db01 10.0.0.51 172.16.1.51 mariadb-server

web01配置

# 安装nginx服务
[root@web01 ~]# yum install -y nginx
# 安装php-fpm 
[root@web01 ~]# tar xf php_nginx.tgz
[root@web01 ~]# cd root/nginx_php/
[root@web01 nginx_php]# rpm -Uvh *rpm
# 创建www用户和用户组,统一环境
[root@web01 ~]# groupadd www -g 666
[root@web01 ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
# 修改nginx和PHP-fpm的启动用户
[root@web01 ~]# vim /etc/nginx/nginx.conf


user  www;
[root@web01 ~]# vim /etc/php-fpm.d/www.conf 
user = www
; RPM: Keep a group allowed to write in log dir.
group = www


# 编辑nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/discuz.com.conf

server {
        listen 80;
        server_name discuz.com;

        location / {
                root /code/discuz;
                index index.php index.html;
        }

        location ~ \.php$ {
                root /code/discuz;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
# 检测语法
[root@web01 ~]# nginx -t
# 启动nginx和php-fpm
[root@web01 ~]# systemctl start nginx php-fpm

# 创建配置文件对应的站点目录
[root@web01 ~]# mkdir -p  /code/discuz
# 进入站点目录现在discuz
[root@web01 ~]# cd /code/discuz/
[root@web01 discuz]#  wget http://test.driverzeng.com/Nginx_Code/Discuz_X3.3_SC_GBK.zip

# 解压
[root@web01 discuz]# unzip Discuz_X3.3_SC_GBK.zip
# 授权
[root@web01 discuz]# chown www.www /code -R
# 移动upload目录下内容到当前目录
[root@web01 discuz]# mv upload/* .

# 域名解析

db01配置

# 下载mysql的小伙伴mariadb
[root@db01 ~]# yum install -y mariadb-server

# 启动并加入开机自启
[root@db01 ~]# systemctl start mariadb.service
[root@db01 ~]# systemctl enable mariadb.service
# 给root用户添加密码
[root@db01 ~]# mysqladmin -uroot password '123'
# 登录
[root@db01 ~]# mysql -uroot -p123
# 创建库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)
# 创建管理库的用户和密码
MariaDB [(none)]>  grant all on discuz.* to discuz@'%' identified by '123'
    -> ;
Query OK, 0 rows affected (0.01 sec)
#  浏览器访问



# 修改nginx配置文件
[root@web01 conf.d]# cat discuz.com.conf 
server {
        listen 80;
        server_name discuz.com;

        location / {
                root /code/discuz;
                index index.php index.html;
        rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
        rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
        rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
        rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
        rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
        rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
        rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
        rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
        if (!-e $request_filename) {
        return 404;
}

	}

        location ~ \.php$ {
                root /code/discuz;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

posted @ 2020-07-23 17:06  王顺子  阅读(528)  评论(0)    收藏  举报