nginx部署不同项目在不同端口

一台服务器上的 nginx 下可能跑着很多项目,如果把很多项目都写到同一个配置文件里 ,会导致后期难以查看与管理

默认配置文件

路径:/etc/nginx/nginx.conf

http {

server {
location / {
root /usr/share/nginx/dist;
index index.html index.htm;
}
}

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

## 多项目配置文件
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

如果直接在 nginx.conf 文件中添加,配置文件会显得比较臃肿且不好维护。此时可以通过 include 的方式,为每个服务建立 nginx 的配置文件,这样也便于后期维护。

在 /etc/nginx/conf.d/ 目录下新建配置文件 ecs.conf

touch ecs.conf

ecs.conf

路径:/etc/nginx/conf.d/ecs.conf

server {
listen 90;

access_log /var/log/nginx/accessecs.log;
error_log /var/log/nginx/errorecs.log;

location / {
# 代码打包路径
root /usr/share/nginx/ecs/;
index index.html index.htm;
# 必写 不然会404
try_files $uri $uri/ /index.html;
}
}

测试配置文件中是否存在语法错误

nginx -t

重新加载配置文件

nginx -s reload
Author: Jiayi Yang
Link: https://jiayiy.github.io/2021/07/01/nginx/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.