nginx配置302跳转

11,302 阅读1分钟

http的302跳转是在工作中经常遇到的一个场景,有时候需要自己搭建服务器支持302来测试,利用nginx可以很方便的搭建起来。 下面时nginx的配置

#user  nobody;
worker_processes  4;

events {
    worker_connections  1024;
}


http {
    #include       mime.types;
    #default_type  application/octet-stream;
    
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;

    server {
        client_max_body_size 4G;
        listen       8089;
        server_name  www.wawa.com;
        root /data;

        location / {
            autoindex  on;
            autoindex_exact_size on;
            autoindex_localtime on;
        }
    	location ~ /*.txt$ {
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
            rewrite ^/(.*) http://www.baidu.com redirect;
    	}
        location ~ /*.mp4$ {
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
            #rewrite ^/(.*) http://www.haha.com/302cache/$1 redirect;
            rewrite ^/(.*) http://192.168.116.128/302cache/$1 redirect;
        }
    }
    server {
        client_max_body_size 4G;
        listen 80;
        server_name www.haha.com;
        root /data;
        
        location / {
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
        }	
    }
}

URL指定

location /     # 默认路径
location ~ /*.txt$    # 正则匹配,匹配以.txt结尾的文件

指定302跳转

跳转到其他服务器

location ~ /*.txt$ {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
    rewrite ^/(.*) http://www.baidu.com redirect;   # 访问任何以.txt结尾的程序都将跳转到百度
}

跳转到当前服务器的其他端口

location ~ /*.mp4$ {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
    #rewrite ^/(.*) http://www.haha.com/302cache/$1 redirect;  # 可以指定域名,也可以指定IP
    rewrite ^/(.*) http://192.168.116.128/302cache/$1 redirect;
}