日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Nginx在Windows下载安装启动与配置前后端请求代理

發布時間:2025/3/19 windows 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx在Windows下载安装启动与配置前后端请求代理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

Nginx入門教程-簡介、安裝、反向代理、負載均衡、動靜分離使用實例:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/103101304

前端是用Vue做的項目,后端是SpringBoot,怎樣將前后端項目部署在Windows服務器上并使用Nginx進行代理。

Nginx下載地址

http://nginx.org/en/download.html

這里點擊相應版本的Windows版本

?

下載之后是一個壓縮包,將其解壓到服務器上的某個目錄

?

Nginx代理配置

進入到上面解壓的conf目錄下,編輯Nginx的配置文件nginx.conf

?

找到server節點

?

首先這里的listen下的端口就是代理前的接口,要與前面前端項目的vue.config.js中的端口一致。

??? server {listen?????? 70;server_name? 10.229.36.158;

然后下面的server_name是你服務器的ip,這里即使是使用的本地也建議不要用localhost,避免修改hosts文件導致的問題。

所以這里就直接設置你要部署項目的服務器的ip。

然后下面的location /下面配置的就是代理前前端靜態資源的路徑等。

root 對應的就是在服務器上前端資源的dist目錄的全路徑,即代表根路徑。

?

下面的兩個配置保持默認不要更改,配置的是防止404和入口頁面。

??????? location / {root?? D:/www/kaoqin/dist/;try_files $uri $uri/ /index.html;index? index.html index.htm;}

然后再下面的location /prod-api/ 就是配置的代理后的地址。

??location /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://localhost:8080/;}

這里的 /prod-api/就是跟前面前端項目設置代理的路徑重寫一致。

下面的一些是設置請求頭等,防止出現跨域問題。

然后最下面的proxy_pass就是設置的代理后的地址,即你的服務器上后臺接口的url。

通過上面兩個配置就能實現在服務器上所有的請求

只要是通過

http://10.229.36.158/70/dev-api/

發送過來的請求全部會被代理到

http://localhost:8080/

下。這樣就能實現前后端項目的請求代理。

完整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;server {listen?????? 70;server_name? 10.229.36.158;#charset koi8-r;#access_log? logs/host.access.log? main;location / {root?? D:/www/kaoqin/dist/;try_files $uri $uri/ /index.html;index? index.html index.htm;}location /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://localhost:8080/;}# 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

來到上面Nginx解壓之后的目錄下(服務器上)即含有nginx.exe的目錄下,在此處打開命令行

start nginx.exe

啟動nginx

?

如果對nginx的配置文件進行修改的話

nginx -s reload

如果沒配置環境變量或者提示不行的話前面使用nginx.exe的全路徑。

正常停止或關閉Nginx:

nginx -s quit

啟動Nginx成功后打開瀏覽器驗證,輸入

http://10.229.36.158:70/

如果能出現頁面,即對應的前端靜態資源的index.html的頁面,并且能顯示驗證碼,驗證碼是通過代理后的

后臺接口獲取。那么就是代理成功,記住不要關閉此nginx的命令行。

?

如果訪問服務器上的地址不成功后檢查70端口是否開放

控制面板-防火墻-高級設置

?

入站規則-新建規則-端口,添加70

?

點擊下一步

?

選擇允許連接

?

配置連接域點擊下一步

?

設置名稱點擊保存。

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的Nginx在Windows下载安装启动与配置前后端请求代理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。