nginx 安装
Linux CentOS
bash
sudo yum install nginx
Linux Ubuntu
bash
sudo apt-get install nginx
MacOS
bash
brew install nginx
系统服务
bash
# 开机启动
sudo systemctl enable nginx
# 启动
sudo systemctl start nginx
bash
# 重新加载配置
nginx -s reload
# 停止
nginx -s stop
# 检查配置文件
nginx -t
# 查看 nginx 版本
nginx -v
解决Router History模式刷新 NotFound
nginx
location / {
root "E:\WorkSpaces\www"; # /home/www
index index.html index.htm;
try_files $uri $uri/ @router;
}
location @router {
rewrite ^(.*)$ /index.html last;
}
SSL配置
nginx
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 443 ssl;
server_name xxx.com www.xxx.com;
#ssl证书的pem文件路径
ssl_certificate /home/ssl/xxx.com.pem;
#ssl证书的key文件路径
ssl_certificate_key /home/ssl/xxx.com.key;
location / {
root /home/web_server/static;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name xxx.com www.xxx.com;
#将请求转成https
rewrite ^(.*)$ https://$host$1 permanent;
}
}
代理配置 websocket
nginx
location / {
# 代理到本地 8080 端口
proxy_pass http://127.0.0.1:8080;
# 使用 HTTP/1.1
proxy_http_version 1.1;
# 设置 X-Real-IP 头
proxy_set_header X-Real-IP $remote_addr;
# 设置 Upgrade 头
proxy_set_header Upgrade $http_upgrade;
# 设置 Connection 头
proxy_set_header Connection "upgrade";
# 设置 Host 头
proxy_set_header Host $host;
}
配置 gzip
nginx
gzip on;
# 最小压缩长度
gzip_min_length 1000;
# 压缩类型
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 application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
# 是否添加vary头
gzip_vary on;
# 缓冲区大小
gzip_buffers 32 4k;