1. 程式人生 > 實用技巧 >Nginx在Windows下載安裝啟動與配置前後端請求代理

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

點選下一步

選擇允許連線

配置連線域點選下一步

設定名稱點選儲存。