1. 程式人生 > 實用技巧 >linux系統rewrite重定向及搭建discuz

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;
        }
}