跳到主要内容

Nginx安装部署

一、安装nginx

(1)添加 nginx 官方提供的 yum 源(需要联网且时间较长)

rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.24.0-1.el7.ngx.x86_64.rpm

使用 yum 安装 nginx

yum install nginx

查看版本

rpm -qa | grep nginx

yum方式安装nginx,它的安装根目录为/etc/nginx 静态资源由Nginx托管,外网必须通过正确的Url才能访问到服务器上的静态资源,而url路径跟文件的存放路径有关,可以通过修改nginx的配置文件实现自定义文件路径配置。 配置nginx

二、配置nginx

nginx的配置文件路径(以本文安装方式):

/etc/nginx/nginx.conf

两个关键点 1、修改配置文件中的端口(自己开放的端口是哪个就用哪个,比如8088) 2、配置文件资源访问节点,root和alias root:root指定的目录是上级目录,path匹配的整个路径会追加,即root+path; alias:alias指定的目录必须带/,path匹配后面的内容会在alias指定的目录下查找,即alias+匹配到path路径后面的部分。

nginx.conf标准文件

    worker_processes  1;
# 以上是全局模块
# worker_processes的值越大,Nginx并发能力越强

events {
worker_connections 1024;
}
# worker_connections的值越大,Nginx并发能力越强

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;

client_body_buffer_size 50m;
client_max_body_size 500m;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

server {
listen 80;
server_name 127.0.0.1;
client_body_buffer_size 128k;
client_max_body_size 500m;
access_log on;

location /{
root /usr/share/nginx/html/;
index index.html index.htm;
access_log off;
}

location /FileServer/ {
proxy_pass http://xx.xx.xx.62:30090;
}

location ~ /FileServer/*.*\.(js|css)?$ {
proxy_pass http://xx.xx.xx.62:30090;
}
# 荣科管理平台-前端演示测试环境
location /jsbos-logistics-web {
#前端文件存放目录
root /usr/share/nginx/html/;
index index.html index.htm;
access_log off;
}


#开发平台标准版
location /jsbos_logistics_serve/ {
#后端服务访问路径
proxy_pass http://xx.xx.xx.62:30000/;
proxy_connect_timeout 600;
proxy_read_timeout 600;

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
access_log on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # 最后两行的set_header表示将http协议头升级为websocket协议
proxy_set_header Connection "upgrade";


}
}
}

Nginx ssl/Https配置


server {
listen 443 ssl; # 监听HTTPS请求的端口并启用SSL
server_name ***.***.com; # ‘修改为自己的域名’配置该服务器块对应的域名

#ssl文件
ssl_certificate /var/log/nginx/cert/gl.bringspring.com.pem;
ssl_certificate_key /var/log/nginx/cert/gl.bringspring.com.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_prefer_server_ciphers on;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

}

重启 Nginx 让新的配置生效

nginx -s reload

三、设置nginx服务

#配置为开机启动 systemctl enable nginx

启动redis

systemctl start nginx

查看redis状态

systemctl status nginx