• 在Linux中对Nginx配置rewrite跳转


    nginx安装在IP为x.x.x.x的服务器上

    rewrite语法:

    rewrite  <正则表达式>  <指定替换的内容>  

    注释:使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位(redirect返回302临时重定向和permanent返回301永久重定向)实现URL重写以及重定向。rewrite只能放在server{}、location{}、if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用。

    rewrite执行顺序:

    (1)执行server 块里面的rewrite指令。

    (2)执行location匹配。

    (3)执行选定的location中的rewrite指令。

    nginx安装

    第一步,安装编译工具及库文件。

    命令:yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel pcre pcre-devel

    第二步,安装Nginx。

    # 下载Nginx安装包

    命令:cd /usr/local/src/

    wget http://nginx.org/download/nginx-1.20.1.tar.gz

    # 解压Nginx安装包

    命令:tar zxvf nginx-1.20.1.tar.gz

    # 编译安装Nginx

    命令:cd nginx-1.20.1

    ./configure \

    --prefix=/usr/local/nginx \

    --sbin-path=/usr/sbin/nginx \

    --conf-path=/etc/nginx/nginx.conf \

    --error-log-path=/var/log/nginx/error.log \

    --http-log-path=/var/log/nginx/access.log \

    --pid-path=/var/run/nginx.pid \

    --lock-path=/var/run/nginx.lock \

    --http-client-body-temp-path=/var/tmp/nginx/client \

    --http-proxy-temp-path=/var/tmp/nginx/proxy \

    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \

    --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

    --http-scgi-temp-path=/var/tmp/nginx/scgi \

    --with-pcre \

    --with-http_v2_module \

    --with-http_ssl_module \

    --with-http_realip_module \

    --with-http_addition_module \

    --with-http_sub_module \

    --with-http_dav_module \

    --with-http_flv_module \

    --with-http_mp4_module \

    --with-http_gunzip_module \

    --with-http_gzip_static_module \

    --with-http_random_index_module \

    --with-http_secure_link_module \

    --with-http_stub_status_module \

    --with-http_auth_request_module \

    --with-mail \

    --with-mail_ssl_module \

    --with-file-aio \

    --with-http_v2_module \

    --with-threads \

    --with-stream \

    --with-stream_ssl_module

    make && make install

    # 查看nginx版本

    命令:/usr/sbin/nginx -v

    或    /usr/sbin/nginx -V

    若结果显示“nginx version: nginx-1.20.1”,则nginx安装完成。

    nginx配置

    第一步,创建 Nginx 运行使用的用户nginx。

    命令:useradd -s /sbin/nologin -M nginx

    ( Nginx服务的默认用户是nobody ,为了安全更改为nginx,在配置文件中启用user nginx nginx;)

    第二步,修改nginx.conf配置文件。

    编辑/etc/nginx/nginx.conf,nginx.conf修改内容如下:

    1. user nginx nginx;  #用户名设置为刚刚创建的用户名
    2. worker_processes  4; #允许生成的进程数,默认为1
    3. worker_cpu_affinity 0001 0010 0100 1000;
    4. error_log  /var/log/nginx/error.log info; #日志位置和级别
    5. pid      /var/run/nginx.pid; #指定nginx进程运行文件存放地址
    6. worker_rlimit_nofile 102400; #最大连接数,默认为512
    7. events {
    8.       use epoll; #事件驱动模型
    9.       worker_connections 102400; #最大连接数,默认为512
    10.       accept_mutex off; #设置网路连接序列化,防止惊群现象发生,默认为on
    11.       multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
    12. }
    13. http {
    14. ...
    15. }

    第四步,检查配置文件nginx.conf的正确性。

    命令:/usr/sbin/nginx -t

    若结果显示“nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)  nginx: configuration file /etc/nginx/nginx.conf test failed”,则说明服务无法启动。可以使用命令“mkdir -p /var/tmp/nginx”创建目录,然后再次运行命令“/usr/sbin/nginx -t”就可以了。

    若结果显示“nginx: configuration file /etc/nginx/nginx.conf test is successful”,则说明nginx安装和配置成功。

    nginx启动和访问站点

    第一步,启动nginx。

    命令:/usr/sbin/nginx

    第二步,检查是否已经启动。(查看是否有进程)

    命令:ps -ef | grep nginx

    结果的第一行显示“nginx:master process”,nginx已经启动。

    注意:nginx:master process后面有一个路径,这就是nginx的安装路径。

    第三步,访问站点。

    从浏览器访问已经配置好的站点IP,如果页面显示“Welcome to nginx!”,则说明Nginx已经安装及配置好了。

    nginx关闭和重启

    第一步,关闭nginx。

    命令:/usr/sbin/nginx -s stop

    第二步,配置文件修改后,需要指定配置文件进行重启。

    如果nginx服务已经停止,那就需要把nginx服务启动。

    命令:/usr/sbin/nginx  -c /etc/nginx/nginx.conf

    重启nginx服务必须是在nginx服务已经启动的情况下进行,因为这时,/var/run中存在nginx.pid文件。

    命令:/usr/sbin/nginx  -s  reload

    nginx配置rewrite跳转-基于域名的重写

    在/etc/nginx/nginx.conf文件中配置,对http://www.mmdxy.com/test/xy/index.php?a=1&b=2进行基于域名的重写,只会对旧域名http://www.mmdxy.com重写,旧域名不能废除,且后面的参数不变。

    格式:http://旧域名/url ——> http://新域名/url

    rewrite  正则表达式  http://新域名/$1  标记位

    编辑nginx.conf文件,在server{}中加入下面代码:

    1. if($host = “www.mmdxy.com”)  {
    2.     # 要加上http://
    3. rewrite ^/(.*)$  http://新域名/$1  permanent;
    4. }

    注释:

    1. ^/   代表根路径开头;
    2. (.*) 代表获取从根路径后面的url路径;
    3. $    代表结尾;
    4. 全域名重写一定要加上http://
    5. $1   代表在前面()里匹配到的字符串;
    6. permanent 代表标识位。

    接着在hosts文件修改为

    虚拟机ip  旧域名  新域名

    最后重新启动nginx,若在浏览器中输入http://www.mmdxy.com/test/xy/index.php?a=1&b=2,网页跳转到http://新域名/$1界面,则证明URL重写成功。

    nginx配置rewrite跳转-基于旧域名跳转到新域名后面加目录

    在/etc/nginx/nginx.conf文件中配置,把http://abc.mmdxy.com/test跳转为http://www.mmdxy.com/abc/test

    格式:http://旧域名/旧url ——> http://新域名/新url

    rewrite  正则表达式  http://新域名/新目录/$1  标记位

    编辑nginx.conf文件,在server{}中加入下面代码:

    1. localtion ~ ^/(aaa|bbb|ccc)  {
    2.     # 要加上http://
    3. rewrite ^/(.*)$  http://www.mmdxy.com/abc/$1 permanent;
    4. }

    接着在hosts文件修改为

    虚拟机ip  旧域名  新域名

    最后重新启动nginx,若在浏览器中输入http://abc.mmdxy.com/test/aaa,网页跳转到http://www.mmdxy.com/abc/$1界面,则证明URL重写成功。

    nginx配置rewrite跳转-基于匹配参数的重写

    在/etc/nginx/nginx.conf文件中配置,把http://www.mmdxy.com/test/xy/index.php?a=1&b=2跳转为http://www.mmdxy.com

    格式:http://域名/url ——> http://域名

    rewrite  正则表达式  http://域名  标记位

    编辑nginx.conf文件,在server{}中加入下面代码:

    1. if($request_uri ~ ^/index.php$)  {
    2.     rewrite ^/(.*)  http://www.mmdxy.com  redirect;
    3. }

    注释:

    1. ​$request_uri  代表包含请求参数的原始URI,不包含主机名,如http://www.mmdxy.com/test/xy/index.php?a=1&b=2中的/test/xy/index.php?a=1&b=2
    2. $uri  代表当前的请求URI,不包含任何参数,如http://www.mmdxy.com/test/xy/index.php?a=1&b=2中的/test/xy/index.php;
    3. redirect  代表标识位。

    最后重新启动nginx,若在浏览器中输入http://www.mmdxy.com/test/xy/index.php?a=1&b=2,网页跳转为http://www.mmdxy.com且可以正常访问,则证明URL重写成功。

    nginx配置rewrite跳转-基于客户端IP地址访问的重写

    要求所有客户端IP地址访问任何内容都显示一个固定维护页面,只有公司内部IP访问正常。

    编辑/etc/nginx/nginx.conf文件,在server{}中加入下面代码:

    1. set $rewrite true;
    2. if($rewrite = true)  {
    3. rewrite ^/ /weihu.html;
    4. }
    5. localtion = weihu.html {
    6.     root /var/www/html;
    7. }

    注释:

    1. 先设置变量名true
    2. 如果$rewrite值为true,则执行重写操作;
    3. 想要从/var/www/html文件夹中获取维护页面内容,就必须先把weihu.html放到/var/www/html文件夹中。
    4. 最后重新启动nginx,若在浏览器中输入网址,网页显示维护页面内容,则证明URL重写成功。

    想要给予某IP地址正常访问权限,在上面代码基础上再添加代码:

    1. if($remote = ‘ip地址’)  {
    2. set $remote false;
    3. }

    最后重新启动nginx,若在浏览器中输入ip地址,网页可以正常访问,则证明URL重写成功。

    不进入nginx根目录即可进行相应的操作

    第一种方法:

    第一步,找到nginx所在的安装目录/usr/local/nginx/sbin,这个目录下有一个名为nginx的文件。

    第二步,创建一个软链接放在全局目录中。相当于在全局环境中设置了一个文件指向依赖的环境目录中。

    命令:cd /usr/local/bin/

    ln -s /usr/sbin/nginx nginx

    现在不进入nginx根目录输入命令,不会再提示command not found。

    第二种方法:

    第一步,新建nginx启动脚本代码。

    在文件夹/etc/init.d中新建名为nginx的文件,然后写入下面代码成为脚本文件。代码如下:

    1. #!/bin/bash
    2. # nginx Startup script for the Nginx HTTP Server
    3. # it is v.0.0.2 version.
    4. # chkconfig: - 85 15
    5. # description: Nginx is a high-performance web and proxy server.
    6. #              It has a lot of features, but it's not for everyone.
    7. # processname: nginx
    8. # pidfile: /var/run/nginx.pid
    9. # config: /usr/local/nginx/conf/nginx.conf
    10. nginxd=/usr/sbin/nginx
    11. nginx_config=/etc/nginx/nginx.conf
    12. nginx_pid=/var/run/nginx.pid
    13. RETVAL=0
    14. prog="nginx"
    15. # Source function library.
    16. . /etc/rc.d/init.d/functions
    17. # Source networking configuration.
    18. . /etc/sysconfig/network
    19. # Check that networking is up.
    20. [ ${NETWORKING} = "no" ] && exit 0
    21. [ -x $nginxd ] || exit 0
    22. # Start nginx daemons functions.
    23. start() {
    24. if [ -e $nginx_pid ];then
    25.    echo "nginx already running...."
    26.    exit 1
    27. fi
    28.    echo -n $"Starting $prog: "
    29.    daemon $nginxd -c ${nginx_config}
    30.    RETVAL=$?
    31.    echo
    32.    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    33.    return $RETVAL
    34. }
    35. # Stop nginx daemons functions.
    36. stop() {
    37.         echo -n $"Stopping $prog: "
    38.         killproc $nginxd
    39.         RETVAL=$?
    40.         echo
    41.         [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
    42. }
    43. # reload nginx service functions.
    44. reload() {
    45.     echo -n $"Reloading $prog: "
    46.     #kill -HUP `cat ${nginx_pid}`
    47.     killproc $nginxd -HUP
    48.     RETVAL=$?
    49.     echo
    50. }
    51. # See how we were called.
    52. case "$1" in
    53. start)
    54.         start
    55.         ;;
    56. stop)
    57.         stop
    58.         ;;
    59. reload)
    60.         reload
    61.         ;;
    62. restart)
    63.         stop
    64.         start
    65.         ;;
    66. status)
    67.         status $prog
    68.         RETVAL=$?
    69.         ;;
    70. *)
    71.         echo $"Usage: $prog {start|stop|restart|reload|status|help}"
    72.         exit 1
    73. esac
    74. exit $RETVAL

    第二步,给予/etc/init.d/nginx文件权限。

    命令:chmod +x /etc/init.d/nginx

    # 设置开机自启

    命令:chkconfig --add nginx

    chkconfig nginx on

    # 检查nginx命令

    命令:service nginx

    Usage: nginx {start|stop|restart|reload|status|help}

    第三步,检查一下脚本是否有用。

    命令:/sbin/chkconfig nginx on

    sudo /sbin/chkconfig --list nginx

    如果结果显示“nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off”,则说明脚本文件有用。

    第四步,服务器重启后,查看nginx是否成功自动启动。

    与“nginx启动和访问站点”中的第二步和第三步一样操作。

    命令:shutdown -r now  #立刻重启

    或    reboot           #立刻重启

    或    init 6           #立刻重启

    或    shutdown -r 10   #过10分钟自动重启

    第五步,nginx启动、关闭以及重启命令。

    命令:ps -ef | grep nginx

    systemctl start nginx

    systemctl stop nginx

    systemctl reload nginx

    service nginx start

    service nginx stop

    service nginx reload

  • 相关阅读:
    【初始RabbitMQ】发布订阅的实现
    03-Redis 凭什么这么快
    NOIP 装箱问题
    在vscode如何利用快捷键选择一样的单词
    五、【React-Router6】路由表 useRoutes() + Outlet
    学习vue3
    【Python opencv 】零基础也能轻松掌握的学习路线与参考资料
    Educational Codeforces Round 155 (Rated for Div. 2)
    react-router-dom6 路由懒加载与组件懒加载
    C++ 反汇编 - 关于函数调用约定
  • 原文地址:https://blog.csdn.net/weixin_43268590/article/details/140104495