Node.js + Nginx WNMP 多域名 多端口 反向代理

Nginx和Node.js是一对很不错的组合,在一个VPS服务器上搭了很多站点,Node.js占了很多端口,可以用Nginx反向代理实现通过不同域名访问不同端口站点的目的。

比如有一个Node.js的程序监听的是8888端口,通过qq.baiezone.com访问127.0.0.1:8888,或者另外一个用织梦CMS搭建的php站点监听的是默认80端口,通过www.baiezone.com访问127.0.0.1。

只需2步即可

第一步:下载安装WNMP套件组合

WNMP套件组合包含NGINX、PHP、MySQL(初始账号root密码password)等

下载地址

wnmp 2.0.1

http://wt.onlinedown.net/down/Wnmp2.0.1.zip

下载完成后解压缩

然后运行压缩包内Wnmp.exe

点击General选项卡下的Start all按钮

查看Windows 任务管理器中如果出现nginx.exe、php-cgi.exe、mysqld.exe三个进程则代表服务器运行正常,如果没有nginx.exe进程请查看80端口是否被其它程序占用,关掉其进程后重新运行nginx.exe

第二部:添加端口代理

打开wnmp目录下的conf目录下的nginx.conf文件

在文件最后添加如下服务

server {  
  listen 80;  
  server_name qq.baiezone.com;  
  location / {  
    proxy_pass http://127.0.0.1:8888;  
  }  
}

server_name 填上域名

proxy_pass 填上ip和端口号

最后启动node程序即可

附上一个完整的nginx.conf

worker_processes  1;

error_log  logs/error.log;
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    access_log  logs/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1.2 TLSv1.1 TLSv1 SSLv3;
        ssl_ciphers ECDHE-RSA-AES128-SHA256:AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH; 
        ssl_prefer_server_ciphers on;
    gzip  on;
    # http server
server {
    listen 80; # IPv4
    server_name localhost;

    ## Parameterization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html;
    index  index.php index.html index.htm;

    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }
        
    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    ## Try the requested URI as files before handling it to PHP.
    location / {

        ## Regular PHP processing.
        location ~ \.php$ {
            root           html;
            try_files  $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location

} # end http server

# https server
server {
    listen 443 ssl;
    server_name localhost;
    ssl_certificate      ssl.cert;
    ssl_certificate_key  ssl.key;

    ## Parameterization using hostname of access and log filenames.
    access_log logs/localhost_access.log;
    error_log logs/localhost_error.log;

    ## Root and index files.
    root html;
    index  index.php index.html index.htm;

    ## If no favicon exists return a 204 (no content error).
    location = /favicon.ico {
        try_files $uri =204;
        log_not_found off;
        access_log off;
    }

    ## Don't log robots.txt requests.
    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    ## Try the requested URI as files before handling it to PHP.
    location / {

        ## Regular PHP processing.
        location ~ \.php$ {
            root           html;
            try_files  $uri =404;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        ## Static files are served directly.
        location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
            expires max;
            log_not_found off;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
            ## Set the OS file cache.
            open_file_cache max=1000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
        }

        ## Keep a tab on the 'big' static files.
        location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
            expires 30d;
            ## No need to bleed constant updates. Send the all shebang in one
            ## fell swoop.
            tcp_nodelay off;
        }
        } # / location
} # end https server

server {
    listen 80;
    server_name qq.baiezone.com;

    location / {
        proxy_pass http://localhost:8888;
    }
}

}
原文地址:https://www.cnblogs.com/baie/p/3029817.html