vue-element-admin | 项目部署及nginx反向代理-401/404问题

一、vue-admin-template项目打包

// 具体打包的命令可见package.json
npm run build:prod

       在项目目录下会生成一个dist文件夹,这个文件夹中内容是我们发布的内容

二、nginx环境部署(Windows环境)

       1、nginx下载地址:http://nginx.org/en/download.html
       2、选择其中一个版本下载,下载好的文件解压即可使用,双击nginx.exe即可运行,没有页面显示,可在后台中看到nginx运行的程序,也可在页面直接输入localhost:80访问可看到nginx的欢迎页
在这里插入图片描述
       3、cmd到nginx目录下输入nginx -v,若出现版本号,则安装成功

三、nginx反向代理(核心)

       1、nginx配置
              打开nginx解压目录下的conf/nginx.conf文件,配置service
              ①以本地做服务器的配置(一般不用)

server {
        listen       8888;#①默认端口是80,如果端口没被占用可以不用修改
        server_name  localhost; #②访问形式,localhost,也可以设置成本地IP等

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root        E:/vue/my_project/dist;#vue项目的打包后的dist

        location / {
            try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
            index  index.html index.htm;
        }
        #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
        #因此需要rewrite到index.html中,然后交给路由在处理请求资源
        location @router {
            rewrite ^.*$ /index.html last;
        }
        #.......其他部分省略
  }

              ②前后端服务器分开的配置(核心)


#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;

    #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  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8088;#①配置访问端口
        server_name  10.201.216.30;#②配置访问方式

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
		#④跨域配置访问代理拦截:当我们在访问到url包含有/prod-api时代理到proxy_pass的地址,如果没有url中不包含/prod-api则访问我们打包中的文件,这样就实现了前后端分离
		location /prod-api/ {
		   proxy_pass http://10.201.88.40:8010/;
	    }
		#③配置访问文件路径root及访问首页index等其他信息
        location / {
            root   E:/DeviceSystem/WorkSpace/testAdmin/vue-admin-template/dist;
            index  index.html index.htm;
			try_files $uri $uri/ /index.html =404;
			add_header Access-Control-Allow-Origin *;
			add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
			add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
			if ($request_method = 'OPTIONS') {
				return 204;
			}
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

       2、404 401问题

              ①404问题:我们将nginx配置完成后,访问时发现页面正常,点击登录时出现404,是由于我们没有解决跨域的问题,即我们没有配置类似如下中的代码:

/prod-api:以vue-element-admin为例,是生产环境.env.production中配置的VUE_APP_BASE_API的值,以区分开发和生产环境,同时在url中会添加/prod-api,所以可以用它作为反向代理的拦截标识
location /prod-api/ {
	proxy_pass http://10.201.88.40:8010/;
}

              ②401问题:访问时有返回,但是会提示NO Authorization未认证,返回的数据也是提示未认证,获取不到正常数据,是由于我们在proxy_pass 结尾没有加“/”导致的,即proxy_pass http://10.201.88.40:8010;就会出现401,加上就不会,具体原因
见https://www.jianshu.com/p/b010c9302cd0(不是很清楚)

发布了24 篇原创文章 · 获赞 1 · 访问量 523

猜你喜欢

转载自blog.csdn.net/Kasey_L/article/details/104947364