Skip to content

Nginx 配置

重要目录及文件说明

目录说明
/etc/nginx目录。nginx 默认配置文件的目录
/etc/nginx/nginx.conf文件。nginx 主配置文件,文件最后配置了自动加载 conf.d 下*.conf 的配置
/etc/nginx/conf.d目录。nginx 的*.conf 多个子配置存放目录
/etc/nginx/conf.d/default.conf文件。nginx 子配置文件,主要用于端口、日志、负载均衡、反向代理等配置。
/usr/sbin/nginx文件。可执行二进制文件,nginx 的启停等执行命令前缀
/usr/share/nginx/html目录。部署静态资源目录
/var/log/nginx目录。日志文件存放目录

修改 /etc/nginx/nginx.conf

配置文件中增加 client_max_body_size 参数。

vi /etc/nginx/nginx.conf

shell
http {
    # 修改大一点,默认 1M 不够用
    client_max_body_size 1024m;
}

修改 /etc/nginx/conf.d/default.conf

  • 修改 default.conf
  • 或者新建 .conf 文件,在 /etc/nginx/conf.d/ 目录下所有 .conf 后缀的文件都会被读取为配置文件。(因为 /opt/nginx/nginx.conf 中配置了 include /etc/nginx/conf.d/.conf)

这里以修改 default.conf 文件为例:

  • rewrite: 转发 http 到 https;
  • ssl_ 相关配置:开启 https 访问,需要提前准备好证书。

vi /etc/nginx/conf.d/default.conf

shell
server {
    listen 80;
    listen  [::]:80;
    server_name aday.fun;
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    listen      443 ssl;
    listen [::]:443 ssl;
    server_name  aday.fun;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/aday.fun.key;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
        # vue 项目这样配置
        #try_files $uri $uri/ /index.html;
        # vitepress 项目这样配置
        try_files $uri $uri.html $uri/ =404;
    }
}

增加 /etc/nginx/conf.d/vita.conf

shell
server {
    listen 80;
    listen  [::]:80;
    server_name vita.aday.fun;
    rewrite ^(.*)$ https://$host$1 permanent;
}

server {
    listen      443 ssl;
    listen [::]:443 ssl;
    server_name  vita.aday.fun;
    ssl_certificate /etc/nginx/ssl/fullchain.cer;
    ssl_certificate_key /etc/nginx/ssl/aday.fun.key;

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080/;
    }
}

重启 nginx 并访问网页

shell
sudo systemctl restart nginx.service

浏览器访问: