nginx部署多个项目

40 篇文章 1 订阅
17 篇文章 0 订阅

前言

实现在一台服务器上使用nginx部署多个项目的方法

查看并修改nginx安装的默认配置文件

在 Linux 操作系统中,Nginx 在编译安装时默认的配置文件路径是 /usr/local/nginx/conf/nginx.conf
如果是通过发行版的包管理器安装,则默认的配置文件路径可能会相应改变,例如在 Ubuntu 下为 /etc/nginx/nginx.conf

user  root;
worker_processes  auto;
worker_rlimit_nofile 65535;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
	use epoll;
	worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
	open_file_cache max=102400 inactive=20s;
    log_format  main  '[$time_local],$remote_addr,$request_time/$upstream_response_time,$remote_user,"$request",'
                      '$status,$body_bytes_sent,"$http_referer",'
                      '"$http_user_agent","$http_x_forwarded_for"';

    access_log logs/access.log  main;
    #access_log on;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length  1000;
    gzip_buffers     16 8k;
    gzip_comp_level 9;
    gzip_proxied any;
    gzip_types  text/plain text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript image/jpeg image/gif image/png application/x-shockwave-flash;
    #add_header ServerIP 192.169.18.153;
    
	client_max_body_size 500m; 
    
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

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

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


    
    include       conf/*.conf;	
}

添加、修改或删除 Nginx 的配置,可以编辑该文件,并使用 nginx -s reload 命令重新加载配置文件。
include conf/*.conf; 配置多个项目conf文件

基于域名配置

基于域名配置,前提是先配置好了域名解析。
域名:www.as.com, 配置了2个二级域名:a.as.com、 b.as.com。

配置 a.as.com 的配置文件:

文件路径:vim /usr/local/nginx/conf/conf/a.conf

server {
       	listen 80; # 端口 需要服务器开放端口
   	   	# 域名绑定需要将域名解析A记录到改服务器ip
   	  	server_name a.as.com; # 你的域名 如果需要ip访问请注释该行并改变端口
        
        location / { 
                root /data/a/dist;
                index index.html;
                #解决页面刷新404问题
        		try_files $uri $uri/ /index.html;
        }
}
配置 b.as.com 的配置文件:

文件路径:vim /usr/local/nginx/conf/conf/b.conf

server {
        listen 80;
        server_name  b.as.com;
        
        location / { 
                root /data/b/dist;
                index index.html;
        		try_files $uri $uri/ /index.html;
        }
}

配置node项目绑定域名

server {
  listen 80;
  server_name api.as.com;
  location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host  $http_host;
      proxy_set_header X-Nginx-Proxy true;
      proxy_set_header Connection "";
      proxy_pass       http://0.0.0.0:3000;  
      proxy_read_timeout 18000; # 设置超时
  }
}

HTTPS(SSL)配置

server {
    listen       443 ssl; # 端口
    server_name api.as.com;  # 域名
 
    ssl_certificate     conf/cert/xxx.pem # 证书路径 pem or crt;
    ssl_certificate_key  conf/cert/xxx.key; # 私钥
 
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
 
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
 
    location / {
        # 这里可以配置静态服务器 or 代理
    }
}

server {
    listen 80;
    listen 443 ssl http2;
    server_name www.as.com;
	#rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
	
	root /usr/local/app/juroom-admin/;
	index  index.html index.htm;
	try_files $uri $uri/ /index.html; 
	
    charset utf-8;
	access_log logs/www.as.com.access.log main;
	error_log logs/www.as.com.error.log;
    
	ssl_certificate conf/cert/www.as.com.pem;
	ssl_certificate_key conf/cert/www.as.com.key;
	ssl_session_timeout 5m;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
	client_max_body_size 100m; #上传文件大小限制
	
	
	location / {
	    root /usr/local/app/juroom-admin/;
	    index  index.html index.htm;
        #解决页面刷新404问题
    	try_files $uri $uri/ /index.html; 

	    if ($request_filename ~* .*\.(?:htm|html)$) {
	        #add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate";
	        add_header Cache-Control no-cache;
	    }
	    if ($request_filename ~* .*\.(?:js|css)$) {
	        expires 30d;
	    }

	    if ($request_filename ~* .*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) {
	        expires 30d;
	    }

	}

}

在 /usr/local/nginx/conf/conf/cert 里放入 .pem 与 .key 文件

nginx.conf常用属性讲解

#定义Nginx运行的用户和用户组
user www www;

#nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8;
 
#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/error.log info;

#进程pid文件
pid /usr/local/nginx/logs/nginx.pid;

#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
    #常用配置
    #设定通过nginx上传文件的大小
    client_max_body_size 8m;

    #长连接超时时间,单位是秒
    keepalive_timeout 120;

    #默认文件类型
    default_type application/octet-stream;
    #默认编码
    #charset utf-8;

    #负载均衡配置
    upstream as.test {
     
        #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
        server 192.168.80.121:80 weight=3;
        server 192.168.80.122:80 weight=2;
        server 192.168.80.123:80 weight=3;

        #nginx的upstream目前支持4种方式的分配
        #1、轮询(默认)
        #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
        #2、weight
        #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
        #例如:
        #upstream bakend {
        #    server 192.168.0.14 weight=10;
        #    server 192.168.0.15 weight=10;
        #}
        #3、ip_hash
        #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
        #例如:
        #upstream bakend {
        #    ip_hash;
        #    server 192.168.0.14:88;
        #    server 192.168.0.15:80;
        #}
       
    }
     
    #虚拟主机的配置
    server {
        #监听端口
        listen 80;
        
        #域名可以有多个,用空格隔开
        server_name www.w3cschool.cn w3cschool.cn;
        index index.html index.htm index.php;
        root /data/www/w3cschool;
         
        #定义本虚拟主机的访问日志  
        access_log  /usr/local/nginx/logs/host.access.log  main;
        access_log  /usr/local/nginx/logs/host.access.404.log  log404;
         
        #对 "/test" 启用反向代理  
        location /test {
            # 所有请求了  ip:port/test/** 的接口 都会转发到 as.test 负载配置去
            proxy_pass as.test;
            # 或者直接配置地址
            # proxy_pass http://127.0.0.1:88;

            #允许客户端请求的最大单文件字节数
            client_max_body_size 10m;
            #后端服务器连接的超时时间_发起握手等候响应超时时间
            #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_connect_timeout 90;

            #后端服务器数据回传时间(代理发送超时)
            #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
            proxy_send_timeout 90;

            #连接成功后,后端服务器响应时间(代理接收超时)
            #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)
            proxy_read_timeout 90;
        }

        
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_孤傲_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值