要实现他人经过我本机ip访问项目,使用nginx技术搭建服务,指向本地前端包,而且代理后台服务器请求数据,这样先后台都有了
(另外一种方法改一行代码就能够,只不过是前端人员改代码,页面会实时变化,请看下两篇篇文章)php
前端代码(页面静态资源) ---> 项目dist路径下的前台包
服务器(硬件) ---> 本机(你的电脑)
web服务器(服务器软件) ---> nginx技术
代理服务器(后台服务器) ---> 请求数据的服务器html
一、nginx下载安装,直到任务进程中有前端
二、修改配置文件,指向本地项目的前台包,请求后台服务器地址
进入安装nginx文件的目录,D:nginx-1.16.1
修改D:nginx-1.16.1conf里面的nginx.conf和新增文件夹myconf中有个文件myconf.confvue
#####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; # } #} }
三、启动本地web项目,
进入cmd:start nginx : 启动nginx服务 nginx.exe
nginx -s reload :修改配置后从新加载生效(启动以后此命令才会生效)
输入localhost:8090访问项目,或者输入你本机IP:8090访问(其余人必须用此访问),例如:xx.x.xxx.xxx:8090nginx
Ps:我三个月前还配置过其余项目A,用的端口是8091,若是不使用nginx -s reload命令重启项目,仅仅使用nginx.exe的话,即便配置文件中没有配置项目A服务,仍然能够经过我本机ip:8091访问项目A,输入xx.x.xxx.xxx:8091会出现项目A
若是出现请求禁止访问错误等,请用postman测试一下你代理的后台服务器是否是通的web
1.https://www.cnblogs.com/a120608yby/p/10096948.htmlapi
vue项目解决nginx方向代理页面404问题服务器
2.https://www.jianshu.com/p/7d33d0330322session
try_files $uri $uri/ @router; 理论解析app
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; } }