如何在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;
    }
  }

}

发布了56 篇原创文章 · 获赞 32 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/hry1243916844/article/details/103462909