有时候你可能需要在一个服务器上,用 nginx 发布两个前端项目。并且这两个前端项目使用不同的域名,域名都绑定80端口。本文说明此场景下的操作过程。Nginx使用的是 Docker 官方镜像。本文依据的操作系统是 Linux Centos7.3 。

假设 yoursite.com 是你的网站域名。
admin.yoursite.com 是你的管理系统的域名。
你的网站前端代码发布地址是: /home/your/path/projects/dist。
你的管理系统前端代码发布地址是: /home/your/path/projects/distAdmin。

编写Nginx配置:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 100000;


events {
    worker_connections  2048;
    multi_accept on;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    #gzip  on;

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

    server_tokens off;
    tcp_nopush on;
    tcp_nodelay on;
    access_log off;
    error_log /var/log/nginx/error.log crit;
    keepalive_timeout 10;
    client_header_timeout 10;
    client_body_timeout 10;
    reset_timedout_connection on;
    send_timeout 10;
    limit_conn_zone $binary_remote_addr zone=addr:5m;
    limit_conn addr 100;
    charset UTF-8;
    gzip on;
    gzip_disable "msie6";
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    open_file_cache max=100000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;
    include /etc/nginx/sites-enabled/*;

    server {
        listen 80;
        server_name www.yoursite.com yoursite.com;
        location / {
              root '/home/your/path/projects/dist';
              try_files $uri $uri/ /index.html =404;

        }
    }

     server {
        listen 80;
        server_name admin.yoursite.com;
        location / {
            root '/home/your/path/projects/distAdmin';
            try_files $uri $uri/ /index.html =404;
        }
    }
}

你的nginx配置文件路径是:/home/your/path/config/nginx.conf

利用docker,创建你的 Nginx 容器:

docker run --name nginx \
-v /home/your/path/config/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /home/your/path/projects/dist:/home/your/path/projects/dist \
-v /home/your/path/projects/distAdmin:/home/your/path/projects/distAdmin \
--restart=always \
-p 80:80 -d nginx:1.13

当然,最后别忘了删除Nginx官方镜像中的默认配置文件 default.conf。你可以访问http://blog.csdn.net/zhangchao19890805/article/details/77802811
来获得详细的解释。我在这里不做太多解释,只告诉你怎么做:

docker exec -it nginx bash
cd /etc/nginx/conf.d
rm -rf default.conf
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐