欢迎阅读『Tag标签列表』

nginx

# 前端架构师官方群 QQ 群:634196762

# 一.安装 node

## 1.进入/usr 目录,新建 toos 目录

```
cd /usr && mkdir tools && cd tools
```

## 2.wget 命令下载对应版本的 node 包,此处下载的是 8.11.2 版本

```
wget https://npm.taobao.org/mirrors/node/v8.11.2/node-v8.11.2-linux-x64.tar.gz
```

## 3.解压 node 包,然后更改目录名为 nodejs
`tar xvf node-v10.13.0-linux-x64.tar.xz `//解压缩
`mv node-v10.13.0-linux-x64 nodejs` //重命名
```
tar -zxf node-v8.11.2-linux-x64.tar.gz
```

```
mv node-v8.11.2-linux-x64 nodejs
```

## 4.配置环境变量,使 node 命令可以全局使用

### 进入 etc 目录,编辑 profile 文件

```
cd /etc && vim profile
```

### 在文件最后加上如下代码:

```
export NODE_HOME=/usr/tools/nodejs
export PATH=$NODE_HOME/bin:$PATH
export NODE_PATH=$NODE_HOME/lib/node_modules:$PATH
```

### 回到/目录,执行

```
source /etc/profile
```
## 使用reboot命令重启使其生效
### 命令刷新配置。此时在任意目录执行 node -v,显示当前 node 版本即安装配置成功。

# 二、安装 nginx

## 1.安装相关依赖

```
yum install gcc-c++

yum install -y pcre pcre-devel

yum install -y zlib zlib-devel

yum install -y openssl openssl-devel
```

## 2.使用 wget 命令下载(推荐)

```
wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
```
或者
```
wget -c https://nginx.org/download/nginx-1.16.0.tar.gz
```
## 3.解压 nginx 包

/_ 这里不要擅自改名字 _/

```
tar -zxvf nginx-1.10.1.tar.gz
```

```
cd nginx-1.10.1
```

## 4.使用默认配置

```
./configure
```

## 5.编译安装

```
make && make install
```

## 6.查找安装路径

```
whereis nginx
```

## 7.配置全局变量

```
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
```

### 常用命令:

`启动`

```
nginx
```

`重新加载配置`

```
nginx -s reload
```

`重新启动`

```
nginx -s restart
```

# https配置:
此操作可通过执行 vim /usr/local/nginx/conf/nginx.conf 命令行编辑该文件。
由于版本问题,配置文件可能存在不同的写法。例如:使用 l`isten 443 ssl `代替 listen 443 和 ssl on。
在页面中添加 JS 脚本。
在后端程序中添加重定向。
通过 Web 服务器实现跳转。
Nginx 支持 rewrite 功能。若您在编译时没有去掉 pcre,您可在 HTTP 的 server 中增加 rewrite ^(.*) https://$host$1 permanent;,即可将默认80端口的请求重定向为 HTTPS。修改如下内容:
```javascript
server {
listen 443;
#填写绑定证书的域名
server_name www.domain.com; 
ssl on;
#网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
root /var/www/www.domain.com; 
index index.html index.htm;   
#证书文件名称
ssl_certificate  1_www.domain.com_bundle.crt; 
#私钥文件名称
ssl_certificate_key 2_www.domain.com.key; 
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
   index index.html index.htm;
}
}
server {
listen 80;
#填写绑定证书的域名
server_name www.domain.com; 
#把http的域名请求转成https
rewrite ^(.*)$ https://$host$1 permanent; 
}
`````
# 前端架构师官方群 QQ 群:634196762

# 以下配置学习:

腾讯云申请 ssl https://console.cloud.tencent.com/ssl/detail/YHU6LMvv

## nginx 配置

### 1.安装依赖包

//一键安装上面四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

### 2.下载并解压安装包

//创建一个文件夹
cd /usr/local
mkdir nginx
cd nginx
//下载 tar 包
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz

//进入 nginx 目录
cd /usr/local/nginx
//执行命令
./configure
//执行 make 命令
make
//执行 make install 命令
make install

### 3.配置 nginx.conf

// 打开配置文件

vi /usr/local/nginx/conf/nginx.conf

### 4.启动 nginx

/usr/local/nginx/sbin/nginx -s reload

// 查看 nginx 进程是否启动:

ps -ef | grep nginx

若想使用外部主机连接上虚拟机访问端口 192.168.131.2,需要关闭虚拟机的防火墙:
centOS6 及以前版本使用命令: systemctl stop iptables.service

centOS7 关闭防火墙命令: systemctl stop firewalld.service
随后访问该 ip 即可看到 nginx 界面。

安装完成一般常用命令

进入安装目录中,

命令: cd /usr/local/nginx/sbin

启动,关闭,重启,命令:

./nginx 启动

./nginx -s stop 关闭

./nginx -s reload 重启

# nginx 默认配置 默认的 config

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

}

```

# nginx 文件结构

```javascript
...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    {
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN]
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}

```

```javascript
########### 每个指令必须有分号结束。#################
#user administrator administrators;  #配置用户或者组,默认为nobody nobody。
#worker_processes 2;  #允许生成的进程数,默认为1
#pid /nginx/pid/nginx.pid;   #指定nginx进程运行文件存放地址
error_log log/error.log debug;  #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
    accept_mutex on;   #设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;  #设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;      #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;    #最大连接数,默认为512
}
http {
    include       mime.types;   #文件扩展名与文件类型映射表
    default_type  application/octet-stream; #默认文件类型,默认为text/plain
    #access_log off; #取消服务日志
    log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
    access_log log/access.log myFormat;  #combined为日志格式的默认值
    sendfile on;   #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

    upstream mysvr {
      server 127.0.0.1:7878;
      server 192.168.10.121:3333 backup;  #热备
    }
    error_page 404 https://www.baidu.com; #错误页
    server {
        keepalive_requests 120; #单连接请求上限次数。
        listen       4545;   #监听端口
        server_name  127.0.0.1;   #监听地址
        location  ~*^.+$ {       #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
           #root path;  #根目录
           #index vv.txt;  #设置默认页
           proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
           deny 127.0.0.1;  #拒绝的ip
           allow 172.18.5.54; #允许的ip
        }
    }
}
```

# 前端架构师官方群 QQ 群:634196762

 

posted @ 2019-11-03 20:06  兔子零-A酱  阅读(206)  评论(0编辑  收藏  举报