Nginx配置rewrite过程介绍

创建rewrite语句

vi conf/vhost/www.abc.com.conf

#vi编辑虚拟主机配置文件

文件内容

server {

        listen 80;

        server_name abc.com;

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

}

server {

        listen 80;

        server_name www.abc.com;

        location / {

                root /data/www/www;

                index index.html index.htm;

        }

        error_log    logs/error_www.abc.com.log error;

        access_log    logs/access_www.abc.com.log    main;

}

或者

server {

        listen 80;

        server_name abc.com www.abc.com;

        if ( $host != 'www.abc.com'  ) {

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

        }

        location / {

                root /data/www/www;

                index index.html index.htm;

        }

        error_log    logs/error_www.abc.com.log error;

        access_log    logs/access_www.abc.com.log    main;

}

(2)重启服务

确认无误便可重启nginx,操作如下:

nginx -t

#结果显示ok和success没问题便可重启

service nginx restart

(3)查看跳转效果

打开浏览器访问abc.com

页面打开后,URL地址栏的abc.com变成了www.bawei.com说明URL重写成功。

原文地址:https://www.cnblogs.com/sdfgdrg/p/10000461.html