Nginx解决ip地址拦截问题,Nginx搭建部署,Nginx支持websocket,Nginx支持https

环境准备:Centos7

依赖准备:

yum install gcc-c++  
yum install pcre pcre-devel  
yum install zlib zlib-devel  
yum install openssl openssl--devel
yum install zip unzip

下载安装包和插件:

自行去官网下载nginx  http://nginx.org 我用的是nginx-1.17.8.tar.gz

下载nginx插件:echo-nginx-module-master.zip

 安装nginx:

tar -zxvf nginx-1.17.8.tar.gz
unzip echo-nginx-module-master.zip 
cd nginx-1.17.8
mkdir -p /usr/local/nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
make -j2
make install
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module --add_module=../echo-nginx-module-master
make -j2
make install

配置nginx:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  120;

    #gzip  on;


    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


         location ~ /nginx/.* {
            if ($request_uri ~ ^/nginx/([a-zA-Z0-9]+\:\/\/[0-9a-zA-Z\.\-\w/\:#\$%\?=&]*)) {
                set $url $1;
        #       set $urlparam $2;
            }
        #    echo "url : $url";
        #    echo "param : $urlparam";
            if ($url ~ ^ws(.*)){
                set $url http$1;
            }
            root html;
            index index.html index.htm;

            proxy_http_version 1.1;
            proxy_connect_timeout 20s;
            proxy_read_timeout 120s;
            proxy_send_timeout 120s;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Sec-WebSocket-Version "13";
            # echo "url : $url";
            proxy_pass $url;

        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

        server {
                listen       443 ssl;
                server_name  localhost;
        # resolver 1.2.4.8;
                ssl_certificate      /tmp/cert/cert.pem;
                ssl_certificate_key  /tmp/cert/cert.key;

                ssl_session_cache    shared:SSL:1m;
                ssl_session_timeout  5m;

                ssl_ciphers  HIGH:!aNULL:!MD5;
                ssl_prefer_server_ciphers  on;

                location ~ /nginx/.* {
                if ($request_uri ~ ^/nginx/([a-zA-Z0-9]+\:\/\/[0-9a-zA-Z\.\-\w/\:#\$%\?=&]*)) {
                    set $url $1;
        #       set $urlparam $2;
                }
                if ($url ~ ^ws(.*)){
                    set $url http$1;
                }
        #    echo "url : $url";
        #    echo "param : $urlparam";
                root html;
                index index.html index.htm;
                proxy_http_version 1.1;
                proxy_connect_timeout 20s;
                proxy_read_timeout 120s;
                proxy_send_timeout 120s;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Sec-WebSocket-Version "13";
                proxy_set_header Connection "Upgrade";
                proxy_pass $url;

        }


     }


}

去阿里云获取一个https的证书,免费的就行,将证书放在/tmp/cert/目录下面,并改名为下面两个的名称

/tmp/cert/cert.pem
/tmp/cert/cert.key

启动nginx:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

停止nginx:/usr/local/nginx/sbin/nginx -s stop

请求连接的样子:https://域名/nginx/总被拦截的请求连接

支持 websocket,支持http,需要将nginx部署到一个不会被拦截的域名下面,被拦截的链接不能直接用域名

发布了8 篇原创文章 · 获赞 0 · 访问量 5915
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览