Nginx 部署react以及node 跨域处理

792 阅读1分钟

nginxp配置

server {
    listen  80;     #1.你想让你的这个项目跑在哪个端口
    server_name  127.0.0.1;     #2.当前服务器ip
    # index index.html index.html;
    location / {
        root   html/build;     #3.前端文件的位置(我是直接放在build目录下了) html前面不要加/ 
        try_files $uri $uri/ /index.html;     #4.重定向,内部文件的指向(照写)
    }
    location /hei {    //同一个域名,部署多个项目
        alias html/hei/;    // /必须要加
        try_files $uri $uri/ /index.html;     #4.重定向,内部文件的指向(照写)
    }
    location  /api {  #4.当请求跨域时配置端口转发
	rewrite ^.+api/?(.*)$ /$1 break; # 正则验证
	include uwsgi_params;
       proxy_pass  http://127.0.0.1:7001; #5.转发地址 后台node运行的端口
       }
    }
    

nginx开启gzip压缩代码

    gzip on;
    gzip_buffers 32 4k;
    gzip_comp_level 6;
    gzip_min_length 200;
    gzip_types text/css text/xml application/javascript;
    gzip_vary on;

重启

/usr/local/nginx/sbin/nginx -s reload