您当前的位置:首页 > 计算机 > 系统应用 > Windows

windows nginx反向代理http/tcp协议

时间:01-18来源:作者:点击数:
城东书院 www.cdsy.xyz
  1. Mainline version:nginx主力版本,为开发版
  2. Stable version:稳定版,在实际生产过程中选择此版本进行安装
  3. Legacy version:历史版本
  • 解压后进入目录nginx-1.16.0/conf目录,打开并编辑文件nginx.conf
  • 修改监听端口为8090(默认端口为80),默认位于第36行

#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  65;

    #gzip  on;

    server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

}
  • 进入目录nginx-1.16.0双击nginx.exe启动服务或通过命令行启动服务start nginx,nginx服务启动窗口一闪而过,任务管理器查看是否有nginx.exe进程
  • nginx命令汇总
命令 含义
start nginx 启动nginx
nginx -s stop 快速停止并退出nginx
nginx -s quit 完整有序的停止并退出nginx
nginx -s reload 修改nginx的配置文件nginx.conf后,不需要关闭nginx后重新启动nginx,执行该命令重新加载配置并生效
nginx -s reopen 重新打开日志文件
  • 配置http协议(如webservice)反向代理

#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  65;

    #gzip  on;
    #服务器的集群
    upstream hiip {#服务器集群名字   
       server 192.168.24.114:8080  weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。  
       server 192.168.24.114:8081  weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
    }

    server {
        listen       8090;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            proxy_pass http://hiip;
            proxy_redirect default;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 10;
            proxy_read_timeout 60;
            proxy_send_timeout 60;	
        }

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

}

解读:本机监听端口8090,当有基于http协议连接到8090端口时,插件自动转接到192.168.24.114:8080或192.168.24.114:8081中(负载均衡)

  • 配置tcp协议反向代理

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


stream {
    upstream rabbitmq {
        server 192.168.24.114:5672;
    }
    server {
        listen 5674;
        proxy_pass rabbitmq;
    }
}

解读:本机监听端口5674,当有基于tcp协议(如socket、rabbitmq)连接到5674端口时,插件自动转接到192.168.24.114:5672中


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


stream {
    upstream rabbitmq {
        server 192.168.24.114:5672;
    }
    server {
        listen 5674;
        proxy_timeout 3600s;
        proxy_pass rabbitmq;
    }
	
    upstream rabbitmq01 {
        server 192.168.24.115:5672;
    }
    server {
        listen 5675;
        proxy_timeout 3600s;
        proxy_pass rabbitmq01;
    }
}

解读:本机监听端口5674和5675

当有基于tcp协议(如socket、rabbitmq)连接到5674端口时,插件自动转接到192.168.24.114:5672中

当有基于tcp协议(如socket、rabbitmq)连接到5675端口时,插件自动转接到192.168.24.115:5672中

城东书院 www.cdsy.xyz
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门