nginx 配置文件
nginx.conf
# 一定要将 nobody 改为当前用户,比如我当前用户为 root
user root;
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 65;
#gzip on;
# 配置 http
server {
listen 80;
# 写你的域名,比如百度
server_name www.baidu.com;
# 重定向到 https
rewrite ^(.*)$ https://${server_name}$1 permanent;
}
# 配置 HTTPS server
server {
listen 443 ssl;
# 依旧是写你的域名
server_name www.baidu.com;
# 证书和key
ssl_certificate /root/project/cert/xxx.pem;
ssl_certificate_key /root/project/cert/xxx.key;
# 下面几个复制粘贴
ssl_session_cache shared:SSL:1m;
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;
# 要将前后端分离开来,不然请求直接打到后端不走前端
# 在前端中,我的 baseUrl 为 axios.defaults.baseURL = '/api';也就是说,前端我写 /login,实际打到后端的是 /api/login
# 后端也需要设置好 server.servlet.context-path : /api
# 这里 location 请求直接打到前端
location / {
# npm run build 后的 dist 文件的路径
root /root/project/frontend/dist/;
# history 模式,一定要写这个,不然在刷新后页面会变为空白页,index.html 前面有 /
# 关于这个可参考 https://www.hi-linux.com/posts/53878.html 此文
try_files $uri $uri/ /index.html;
index index.html;
}
# 转发请求到后端,由于我们的前端是用了 /api 的方式,因此需要将前端有请求后端的部分直接打过去
# 通过指定路径将前后端分离开来
location /api/ {
# 打到后端 8081 端口,这里使用 https 是因为我的后端加了证书
proxy_pass https://127.0.0.1:8081;
# 下面几个复制粘贴
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}