Nginx
下载地址:http://nginx.org/en/download.html
安装及使用
下载稳定版Nginx,解压,存放位置不要有中文目录
进到目录下运行 nginx.exe 就行
配置文件默认监听80端口
直接在浏览器运行localhost就能看到效果了
常用命令
# 进入目录
cd /usr/local/nginx/sbin
# 启动
./nginx
# 停止
./nginx -s stop
# 安全退出
./nginx -s quit
# 重新加载配置文件
./nginx -s reload
前后端分离项目部署
配置文件
#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;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 需要被监听的端口号,前提是此端口号没有被占用,否则在重启 Nginx 时会报错。访问前端需要访问这个端口
listen 8080;
# 服务名称,无所谓
server_name cors;
root html/dist;
client_max_body_size 20m;
client_body_buffer_size 128k;
# 根请求会指向的页面
location / {
try_files $uri $uri/ /index.html;
#index index.html;
}
location /api/ {
# 后端的真实接口。如果项目有后缀,也需要加
proxy_pass http://127.0.0.1:9090/;
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;
proxy_set_header Cookie $http_cookie;
# for Ajax
#fastcgi_param HTTP_X_REQUESTED_WITH $http_x_requested_with;
proxy_set_header HTTP-X-REQUESTED-WITH $http_x_requested_with;
proxy_set_header HTTP_X_REQUESTED_WITH $http_x_requested_with;
proxy_set_header x-requested-with $http_x_requested_with;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 128k;
proxy_buffers 32 32k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
}
}
}
前端处理
前端项目进行打包,例如 Vue
项目打包出来的 dist
,将整个文件夹放到 nginx
目录下的 html
文件夹中
上面的配置文件已经指定了根目录是 html/dist
不要忘记开放端口号
后端处理
正常的运行就好