前言:
要实现他人通过我本机ip访问项目,使用nginx技术搭建服务,指向本地前端包,并且代理后台服务器请求数据,这样前后台都有了
(另一种方法改一行代码就可以,只不过是前端人员改代码,页面会实时变化,请看下两篇篇文章)
思维导图
前端代码(页面静态资源) ---> 项目dist路径下的前台包
服务器(硬件) ---> 本机(你的电脑)
web服务器(服务器软件) ---> nginx技术
代理服务器(后台服务器) ---> 请求数据的服务器
搭建过程:
1、nginx下载安装,直到任务进程中有
2、修改配置文件,指向本地项目的前台包,请求后台服务器地址
进入安装nginx文件的目录,D:nginx-1.16.1
修改D:nginx-1.16.1conf里面的nginx.conf和新增文件夹myconf中有个文件myconf.conf
#####myconf.conf文件配置#####
server {
listen 8090; #监听端口 可以访问127.0.0.1:8090
server_name localhost; #这里要是使用需要配本地的host
#charset koi8-r;
access_log logs/k8s.log;
location / {
root D:/xxx/dist; #你项目的前端打包后的文件夹
try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
index index.html index.htm;
}
#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://xxx:8086; #这里是后台服务器地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#####nginx.conf文件配置#####
#####就是引入自己的配置文件(myconf.conf),覆盖原有的~~~~配置(nginx.conf)#####
#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;
#引入自己定义的config#####重要#####
include myconf/*.conf;
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;
# }
#}
}
3、启动本地web项目,
进入cmd:start nginx : 启动nginx服务 nginx.exe
nginx -s reload :修改配置后重新加载生效(启动之后此命令才会生效)
输入localhost:8090访问项目,或者输入你本机IP:8090访问(其他人必须用此访问),例如:xx.x.xxx.xxx:8090
Ps:我三个月前还配置过其他项目A,用的端口是8091,如果不使用nginx -s reload命令重启项目,仅仅使用nginx.exe的话,即使配置文件中没有配置项目A服务,仍然可以通过我本机ip:8091访问项目A,输入xx.x.xxx.xxx:8091会出现项目A
如果出现请求禁止访问错误等,请用postman测试一下你代理的后台服务器是不是通的
途中bug解决方案:
1.https://www.cnblogs.com/a120608yby/p/10096948.html
vue项目解决nginx方向代理页面404问题
2.https://www.jianshu.com/p/7d33d0330322
try_files $uri $uri/ @router; 理论解析
3.https://www.jianshu.com/p/02ad1f919471
为什么报错404
server {
listen 8090; #监听端口 可以访问127.0.0.1:8090
server_name localhost; #这里要是使用需要配本地的host
#charset koi8-r;
access_log logs/k8s.log;
location / {
root D:/wqy/Project/quality/qualitydetect/web/dist; #你项目的根目录
try_files $uri $uri/ @router;
index index.html index.htm;
}
location @router {
rewrite ^.*$ /index.html last;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
}
#对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
#因此需要rewrite到index.html中,然后交给路由在处理请求资源
location /api {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://****:8880; #****这里填上服务器地址
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。