NGINX部署vue+springboot项目
NGINX部署vue+springboot项目部署Vue首先将vue打包出来的dist文件放到nginx/html下,不是将整个文件夹放进去,而是将文件内的文件放进去[root@hecs-82454 html]# ls50x.htmlcssfavicon.icofontsindex.htmljs然后修改配置文件nginx/conf/nginx.conf...
NGINX部署vue+springboot项目
嫌麻烦不想自己创建项目的,可以自取
链接: https://pan.baidu.com/s/1z-BT1ZR4dN34BY5Oz1QkEg 提取码: 0ge3
部署Vue
首先将vue打包出来的dist文件放到nginx/html下,不是将整个文件夹放进去,而是将文件内的文件放进去
[root@hecs-82454 html]# ls
50x.html css favicon.ico fonts index.html js
然后修改配置文件nginx/conf/nginx.conf,其实,什么也没修改,包括user 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 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
# }
#}
}
再次进入nginx的地址,也就是
http:xxx.xxx.xxx:80,就是我们的vue项目了
点击huaweiyuntest这个链接,正常进入了我们的主页面
然后再将我们的springboot项目启动起来,可以像我一样
[root@hecs-82454 Idea]# java -jar huaweiyun-0.0.1-SNAPSHOT.jar
或者
nohup java -jar xxx.jar &
这样的话后台启动项目,执行ctrl + c也不会中断线程的执行。
启动起来之后我们调用一下自己的接口
http://xxx.xxx.xxx.xxx:9877/huaweiyun/hello
但是出现了一个问题,请求的时候404了!明明同样的配置在本地调试的是没问题呀,而且直接请求后端接口也是没问题,那只能说明是我们nginx的配置文件出问题了
重新检查配置文件,发现少了一些配置,上图我们也看到了,我们的请求都带了一个/api,这个是我们vue项目里为了方便请求后端来做的一个配置,那现在我们就应该为这个/api做一个特殊的location
完整配置如下
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 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /api {
proxy_pass http://localhost:9877/;
}
#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;
# }
#}
}
重新启动nginx后再次请求,成功
更多推荐
所有评论(0)