场景:都说老板是吸血鬼小气压榨员工,确实当过老板的都比较抠门,挣钱不容易...这不为了节省硬件资源服务器也要拼一个,把我们开发的项目前端都放到一个服务器上,后端通过端口可以重新打包启动,前端如何解决呢?放到一个nginx.conf配置多个location /admin太乱了,前端也只能访问一个端口,开动我们神奇的大脑,还好nginx支持多个conf文件加载,来吧展示!

1.重新修改nginx.conf文件,所有的精华都在 include /usr/local/nginx/conf/conf.d/*.conf;

#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;
    include /usr/local/nginx/conf/conf.d/*.conf;

}

nginx-conf.png

2.增加/usr/local/nginx/conf/conf.d 配置文件

all-cong.png

3.放大招了,内容如下:

server {
    listen       8088;
    server_name  localhost;
        #root /home/vue/hx-web-master/dist;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

    location / {
            #root   html;
        root /home/xu.han/autotestplat/web/dist;
        index index.html;
            #if (!-e $request_filename) {
            #    rewrite ^/(.*) /index_prod.html last;
            #    break;
            #}
        #proxy_pass http://192.168.2.78:8000;
        try_files $uri $uri/ /index.html;
    }

    location ^~ /*{
        proxy_pass http://192.168.2.78:8000;
            #add_header Access-Control-Allow-Origin *;
            #add_header Access-Control-Allow-Methods "POST, GET, OPTIONS";
            #add_header Access-Control-Allow-Headers "Origin, Authorization, Accept";
            #add_header Access-Control-Allow-Credentials true;
    }
}

auto-conf.png

4.最后重新加载 *.conf配置文件

nginx-reload.png

Last modification:October 20th, 2020 at 09:06 am
如果觉得我的文章对你有用,请随意赞赏