要實現他人經過我本機ip訪問項目,使用nginx技術搭建服務,指向本地前端包,而且代理後臺服務器請求數據,這樣先後臺都有了
(另外一種方法改一行代碼就能夠,只不過是前端人員改代碼,頁面會實時變化,請看下兩篇篇文章)php
前端代碼(頁面靜態資源) ---> 項目dist路徑下的前臺包
服務器(硬件) ---> 本機(你的電腦)
web服務器(服務器軟件) ---> nginx技術
代理服務器(後臺服務器) ---> 請求數據的服務器html
一、nginx下載安裝,直到任務進程中有
前端
二、修改配置文件,指向本地項目的前臺包,請求後臺服務器地址
進入安裝nginx文件的目錄,D:nginx-1.16.1
修改D:nginx-1.16.1conf裏面的nginx.conf和新增文件夾myconf中有個文件myconf.conf
vue
#####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; } }