nginx绑定多个端口

 

有两种方法:

一、在server段写上2个Listen就可以了.

listen 192.168.0.15:808;
listen 192.168.0.15:8098;
如上,就可以同时监听2个端口了.

二、在 nginx.conf 中配置多个个server即可

user nginx nginx;

worker_processes  1;

worker_rlimit_nofile 65535;

events {
    worker_connections  4000;
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                       '$upstream_addr $upstream_response_time $request_time ';
    access_log  logs/nginx/access.log main;

    sendfile        on;

    keepalive_timeout  65;

        upstream localhost {
        server 127.0.0.1:8080 max_fails=7 fail_timeout=7s;
    } 

    server {
        listen       808;
        server_name  localhost;
                large_client_header_buffers 4 128k;
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers   4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;

#设定查看Nginx状态的地址
        location /nginxstatus{
            stub_status on;
            access_log on;
            auth_basic "nginxstatus";
            auth_basic_user_file htpasswd;
    }

#ftpweb
         location /ftpweb {
           index  index.html index.htm index.jsp;
           proxy_pass  http://192.168.0.16:9081/ftpweb;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header Host $host:$server_port;
        }

#Zabbixweb
         location /webzabbix/ {
           index  index.html index.htm index.jsp;
           proxy_pass  http://192.168.0.15/zabbix/;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header Host $host:$server_port;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen       8098;
        server_name  localhost;
        large_client_header_buffers 4 128k;
        client_max_body_size 300m;
        client_body_buffer_size 128k;
        proxy_connect_timeout 600;
        proxy_read_timeout 600;
        proxy_send_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers   4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;

#开放8098接口地址
         location /interserver {
           index  index.html index.htm index.jsp;
           proxy_pass  http://192.168.0.20:8812/interserver/;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header Host $host:$server_port;
        }

    }

}

这样就就可以一个nginx服务访问808和8098两个端口到不同服务的了
http://192.168.0.15:808/ftpweb
http://192.168.0.15:8098/interserver

补充方法三:

   通过proxy_pass 实现

  • /netdata => 转发19999
  • /lets chat => 转发8000
  • /shipyard => 80是nginx自己的端口,不做转发
location /netdata {
    proxy_pass http://localhost:19999/;
}
location /lets chat { // 这里你问题应该有问题,最好不要有空格
    proxy_pass http://localhost:8000/;
}

记住proxy_pass 地址的最后一个斜杠是关键

  • 访问 /netdata/xxx/bbb时,如果代理地址最后有/,那么会转发到http://localhost:19999/xxx/bbb;
  • 访问 /netdata/xxx/bbb时,如果代理地址最后没有/,那么会转发到http://localhost:19999/netdat...

原文连接:http://blog.51cto.com/meiling/2067988

你可能感兴趣的:(nginx)