linux(centOS)下搭建node服务器四、nginx实现通过域名或特定网址访问项目

上一篇 linux(centOS)下搭建node服务器三、 pm2管理和保持后台运行node应用 讲述了如何使用pm2管理node应用程序和保持后台运行node应用,本篇讲述通过使用nginx实现:通过域名或网址访问项目(去掉端口号)、一台服务器同时支持多项目访问

Nginx安装使用需要依赖一些工具和库,安装Nginx之前需要先安装这些工具和库:

一、安装编译工具和相关的库文件 

执行命令(yum是CentOS中的Shell前端软件包管理器,腾讯云服务器已默认安装):

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

回车如下图开始安装: 

等待20s左右安装完成:

二、安装 PCRE 

PCRE(Perl Compatible Regular Expressions) :perl语言兼容正则表达式,是一个用C语言编写的正则表达式函数库。

安装PCRE的作用是让Nginx支持Rewrite(实现RUL地址的重定向)功能

 

1. 打开网址:https://sourceforge.net/projects/pcre/

ctrl+shift+i(打开开发者工具) 如图:选择.tar.gz结尾的,复制地址(不要/download)

 2.下载 PCRE 安装包、解压文件

回车,等待下载完成

有时候网速不好或其他原因导致重复下载,这个时候会被保存成pcre-8.41.tar.gz.1、pcre-8.41.tar.gz.2等,而不是pcre-8.41.tar.gz。由于pcre-8.41.tar.gz是未下载完成的,会导致后续各种报错,

比如:

执行./configure 会报:cannot find sources (pcre.h.in) in . or ..。

这个时候需要删除之前下载的重新下载解压安装就好了(本人最开始装的8.42,试了各种方法解决不了,被这个问题搞到快疯了,没办法改成8.41版本安装成功的)

执行删除命令:

rm -f + 要删除的压缩包名称
rm -f pcre-8.41.tar.gz

然后重新下载 

wget + 第一步复制的地址
wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz

解压文件

tar -xvzf + 刚才下载的PCRE;这里是:
tar -xvzf pcre-8.41.tar.gz

3.进入安装包目录,编译安装

进入安装包目录:
cd pcre-8.41

编译安装:
./configure
make && make install

4.查看pcre版本

pcre-config --version

PCRE安装成功

三、安装 Nginx

 步骤类似 二、安装 PCRE 

1.打开网址:http://nginx.org/en/download.html,复制下载地址

选取稳定版(注意这个地方域名被省略了,要补全):http://nginx.org/download/nginx-1.14.0.tar.gz

2.下载nginx并解压

wget http://nginx.org/download/nginx-1.14.0.tar.gz

解压

tar -xvzf nginx-1.14.0.tar.gz

3.进入安装包目录,编译安装

进入安装包目录:
cd nginx-1.14.0

编译安装:
./configure --prefix=/usr/local/share/applications/nginx-1.14.0/ --with-http_ssl_module
make && make install

 

4.查看nginx是否安装成功

/usr/local/share/applications/+安装的版本号+/sbin/nginx -v,这里是:

/usr/local/share/applications/nginx-1.13.7/sbin/nginx -v

5.启动nginx

/usr/local/share/applications/nginx-1.14.0/sbin/nginx

成功了!!

6. Nginx 常用的几个命令

启动nginx
/usr/local/share/applications/nginx-1.14.0/sbin/nginx

重新载入配置文件
/usr/local/share/applications/nginx-1.14.0/sbin/nginx -s reload 

重启 Nginx
/usr/local/share/applications/nginx-1.14.0/sbin/nginx -s reopen      
      
停止 Nginx
/usr/local/share/applications/nginx-1.14.0/sbin/nginx -s stop   

其中:'/usr/local/share/applications/nginx-1.14.0/sbin'根据实际情况目录改变
           

 四、修改Nginx配置,实现通过网址访问项目

目标:

1.通过http://xuyakai.xyz访问使用koa2搭建的项目(通过网址去掉端口号访问).项目搭建见:

linux(centOS)下搭建node服务器二、 使用koa2搭建项目

linux(centOS)下搭建node服务器三、 pm2管理和保持后台运行node应用

2.通过http://admin.xuyakai.xyz访问项目rh下的index.html

 

实现:

1.查找nginx目录

find / -name nginx

2.用winscp(文件管理工具 linux(centOS)下搭建node服务器之一、 安装Node.js中有安装使用介绍)打开nginx.conf进行修改

打开原内容如下:


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

}

 

2.修改配置nginx.conf。只放了修改到的地方

http {

    server {
        listen       80;
        server_name  xuyakai.xyz;
        
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass   127.0.0.1:3001;
        }

    }

    server {
        listen       80;
        server_name  admin.xuyakai.xyz;
        root /opt/rh;
        location / {
            # root   html;
            # index  index.html index.htm;
           
        }
    }
}

重启nginx:

/usr/local/share/applications/nginx-1.14.0/sbin/nginx -s reload

 通过网址访问:http://xuyakai.xyz/http://admin.xuyakai.xyz/结果如下,表示成功:

 

注:

1.server中的 listen设置为80 不要设置为其他的端口,因为浏览器默认端口为80可以省略,如果设置为其他的端口则无法省略

2.127.0.0.1:3001中3001是koa2项目使用端口号,和web1/bin/www中设置的保持一致。127.0.0.1回送地址,相当于本机ip

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值