- 后端打包教程
注意:需要先運行redis
2、前端運行教程
2.1安裝依賴
2.2運行
打開瀏覽器查看,地址:http://localhost:80
3、前端打包教程
3.1打包
3.2運行打包好的文件,先找到打包好的文件
這是nginx的文件結構
將打包好的文件放到html目錄下
這是index.html文件所在目錄
配置nginx代理規則
啟動nginx
啟動后進入瀏覽器查看,地址:http://localhost:80/
以下是重啟nginx的命令:nginx -s reload
以下是重啟nginx的命令:nginx -s reload
nginx 啟動
nginx -s stop 停止
nginx -s quit 安全退出
nginx -s reload 重新加載配置文件 如果我們修改了配置文件,就需要重新加載。
ps aux|grep nginx 查看nginx進程
接口異常是因為請求發送不到后端
發現向后端請求 ?“生成驗證碼” ?時發送到了http://localhost/prod-api/captchaImage
而后端端口是8080,本機地址是localhost,也可以用127.0.0.1表示
按ctrl+shift+f鍵,找到生成二維碼的接口
發現這個控制器類沒有配置訪問地址,只有方法配置了訪問地址,所以想要進入該方法,就應該發請求到localhost:80/captchaImage
所以前端應該要配置nginx代理規則:將所有以/prod-api/開頭的請求全部代理到http://127.0.0.1:8080/
比如:http://localhost/prod-api/captchaImage會代理到,會變成http://localhost/captchaImage
個人猜想:應該是把http://127.0.0.1:8080/prod-api/改成了http://127.0.0.1:8080/,后面的內容沒變
配置代碼如下
#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 80; #監聽80端口server_name localhost; #服務器的ipv4地址#charset koi8-r;#access_log logs/host.access.log main;location / { #默認首頁,訪問/時,返回html/dist目錄下的index.html(即訪問localhost:80/時,返回html/dist目錄下的index.html)root html/dist;index index.html index.htm;try_files $uri $uri/ /index.html; #加上這一行,頁面刷新就不會出現404}location /prod-api/ { #表示當請求的路徑以 "/prod-api/" 開頭時,應用后續的配置規則。proxy_set_header Host $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header REMOTE-HOST $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:8080/; #表示將請求轉發到本地的 127.0.0.1:8080 地址。}#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后刷新瀏覽器,成功了