2.1单机
下载地址http://nginx.org/en/download.html
先安装依赖库
安装gcc gcc-c++
yum install -y gcc gcc-c++
安装pcre
yum install -y pcre pcre-devel
安装zlib
yum install -y zlib zlib-devel
安装openssl
yum install -y openssl openssl-devel
将下载的tar.gz压缩包上传至/home/software
进入该目录
解压tar zxvf nginx-1.16.1.tar.gz
进入解压后的目录
cd /home/software/nginx-1.16.1
指定nginx安装目录 ./configure --prefix=/home/software/nginx
./configure --prefix=/home/software/nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-stream
编译make
安装make install
检查安装
cd ../nginx/sbin
./nginx -t
出现
nginx: the configuration file /home/software/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/software/nginx/conf/nginx.conf test is successful即表示nginx安装成功
启动
/home/software/nginx/sbin/nginx
查看是否运行
netstat -npal|grep nginx
删除源文件
cd /home/software
rm -rf nginx-1.16.1
结束nginx进程命令
pkill -9 nginx
2.1.1 配置web和http
nginx配置文件是
vi /home/software/nginx/conf/nginx.conf
修改/home/nginx/conf/conf.d/qt.conf文件
server {
listen 80;
server_name localhost;
access_log /home/software/nginx/logs/access_user.log;
error_log /home/software/nginx/logs/error_user.log;
location / {
root /home/zysco/build;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
index index.html;
try_files $uri $uri/ /index.html;
}
# 静态资源
location ~ .*\.(gif|jpg|jpeg|png|flac|mp3|mkv)$ {
root /home/zysco/build;
}
location /api {
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:30002/api;
client_max_body_size 20m;
}
}
server {
listen 5000;
server_name localhost;
access_log /home/software/nginx/logs/access_user.log;
error_log /home/software/nginx/logs/error_user.log;
location / {
root /home/zysco/h5-build;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
index index.html;
try_files $uri $uri/ /index.html;
}
# 静态资源
location ~ .*\.(gif|jpg|jpeg|png|flac|mp3|mkv)$ {
root /home/zysco/h5-build;
}
location /api {
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:30002/api;
client_max_body_size 20m;
}
}
2.1.2 配置websocket
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
location /ws {
proxy_pass http://localhost:50004/ws;
proxy_read_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location /message {
proxy_pass http://localhost:50003/message;
proxy_read_timeout 300s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
2.1.3 配置文件
/home/software/nginx/sbin/nginx
检查配置文件
/home/software/nginx/sbin/nginx -t -c /home/software/nginx/conf/nginx.conf
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
根据需求修改反向代理设置,然后重新加载配置
/home/software/nginx/sbin/nginx -s reload
Nginx日志目录是
/home/software/nginx/logs
解压前端项目 unzip build.zip
关闭防火墙systemctl disable firewalld
firewall-cmd --state
service firewalld stop
firewall-cmd --state
2.2 Netty服务高可用
安装时配置:需要添加with-stream参数 nginx tcp负载均衡需要
./configure --prefix=/home/software/nginx --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-stream
stream{
upstream netty_path{
server 139.9.160.196:6699 weight=1;
server 116.63.55.55:6699 weight=1;
}
server {
listen 9527;
proxy_pass netty_path;
}
}
/home/software/nginx/sbin/nginx -s reload
2.3 gzip有利有弊
#开启gzip
gzip on;
#启用gzip压缩的最小文件;小于设置值的文件将不会被压缩
gzip_min_length 1k;
# gzip压缩级别 1-10
gzip_comp_level 2;
#进行压缩的文件类型。
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
#是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
2.4 nginx 配置多个静态资源路径
location ~ .*\.(gif|jpg|jpeg|png|flac|mp3|mkv)$ {
root /home/zysco/build;
}
location ~ .*\.apk {
root /home/zysco/app;
}