前言实现在一台服务器上使用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 max102400 inactive20s; 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 Apaches document root # concurs with nginxs 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.confserver { 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.confserver { 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; # 设置超时 } }HTTPSSSL配置server { listen 443 ssl; # 端口 server_name api.as.com; # 域名 # 文件目录 /usr/local/nginx/conf/conf/cert 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 weight3; server 192.168.80.122:80 weight2; server 192.168.80.123:80 weight3; #nginx的upstream目前支持4种方式的分配 #1、轮询默认 #每个请求按时间顺序逐一分配到不同的后端服务器如果后端服务器down掉能自动剔除。 #2、weight #指定轮询几率weight和访问比率成正比用于后端服务器性能不均的情况。 #例如 #upstream bakend { # server 192.168.0.14 weight10; # server 192.168.0.15 weight10; #} #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; } } }