nginx多个配置文件并配置二级域名多个server

  |   0 评论   |   0 浏览

首先修改本机hosts(本地测试)

window路径为: C:\Windows\System32\drivers\etc

Linux路径为:/etc/hosts

image.png

ip为虚拟机的ip地址

到nginx 的html目录下创建

mkdir -p {test,bbb}

并分别创建index.html文件

nginx的主配置文件


#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       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location ~ .*\.(/|html|gif|jpg|jpeg|png|bmp|swf|ico|ttf|woff|css|js|map) {
               root  /usr/share/nginx/html/chaoxing;
               index  index.html index.htm;
                try_files $uri $uri/ /index.html;
  
        }

        location / {
            root   /usr/share/nginx/html/chaoxing;
            # proxy_pass http://192.168.44.130:8080;
            index  index.html index.htm;
                # vue的路由问题
            try_files $uri $uri/ /index.html;
                # nginx默认是不允许post请求的,将405指向200
            error_page 405 =200  http://$host$request_uri;
        }

        location /api {
           proxy_pass  http://192.168.44.130:8080;
        }



        location ^~/resources {
            root myresources;
            autoindex on;
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }



    include /etc/nginx/conf.d/*.conf;




}

include /etc/nginx/conf.d/*.conf; 这一段代码 最重要,用来包含 其他的配置文件。

include /etc/nginx/conf.d/*.conf;

conf.d文件夹下 创建子配置文件

例如

test.conf

 server {
        listen       80;
        server_name  aaa.test.cn;

        location / {
                root  /usr/share/nginx/html/test;
                index  index.html;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

bbb.conf

 server {
        listen       80;
        server_name  bbb.test.cn;

        location / {
                root  /usr/share/nginx/html/bbb;
                index  index.html;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

重启nginx

效果图

image.png

image.png


标题:nginx多个配置文件并配置二级域名多个server
作者:gitsilence
地址:https://lacknb.cn/articles/2020/07/01/1593572122579.html