如何在nginx中配置多個前端項目

首先安裝好ngnix,找到目錄下的html文件夾,將自己打包好的前端項目放在html文件夾下,如下圖:
在這裏插入圖片描述
找到conf>nginx.conf文件,以我框選出來的爲例,配置好想設置的端口號,對應的文件夾名稱。
在這裏插入圖片描述
重啓nginx,訪問http://localhost:2456即可
新的項目複製粘貼代碼,更改端口號和對應的文件夾,詳情看下面的代碼。
我標紅的這兩個項目都是react的,補充兩點:
1.訪問到頁面之後刷新頁面會顯示404,需要加上

try_files $uri /index.html; # 解決刷新404問題

2.配置本地代理規則
因爲這是前後端分離的項目,需要配置接口的ip。
"/api"是我在項目中訪問接口的前綴,根據自己的項目更改即可。

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;

  server {
    listen 2456;
    server_name localhost;

    #charset koi8-r;

    location / {
      root html/webPointCloudApp;
      index index.html index.htm;
      try_files $uri /index.html; # 解決刷新404問題
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }

    #配置本地代理規則
    location /api {
      rewrite ^/api/(.*)$ /$1 break;
      proxy_pass http://XXXXXXXXXXX; #接口地址
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }


    #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 D:\website\WebDevWebsite\build_2019_04_16_d;
    }

    # 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;
    #}
  }

  server {
    listen 2457;
    server_name localhost;

    #charset koi8-r;

    location / {
      root html/pointCloudMarket;
      index index.html;
      try_files $uri /index.html; # 解決刷新404問題
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }

    #配置本地代理規則
    location /api/api {
      rewrite ^/api/(.*)$ /$1 break;
      proxy_pass http://XXXXXXXXXXX; #接口地址
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }

    #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 D:\website\WebDevWebsite\build_2019_04_16_d;
    }

    # 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;
    #}
  }

  server {
    listen 2458;
    server_name localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    location / {
      root html/kuangshan;
      index index.html;
      try_files $uri /index.html; # 解決刷新404問題
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }

    #配置本地代理規則
    location /api {
      rewrite ^/api/(.*)$ /$1 break;
      proxy_pass http://XXXXXXXXXXX; #接口地址
      client_max_body_size 1024M;
      proxy_set_header Host $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;
    }


    #普通項目需要定義一下文件的路徑
    location ^~\.js {
      root html/kuangshan;
    }

    location ^~\.css {
      root html/kuangshan;
    }

    location ^~\.html {
      root html/kuangshan;
    }


    #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 D:\website\WebDevWebsite\build_2019_04_16_d;
    }

    # 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 1234;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    location / {
      root html/admin;
      index index.html;
    }
  }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章