Ngnix 常用配置

2022/03/25 更新 1 次 更新于 2026/08/08 Ngnix 共 749 字,约 3 分钟

Ngnix 常用配置整理

1、文件服务器

nginx.conf 增加 location 配置,nginx 需要有读取文件的权限,文件目录为:/data/file/

server {
    listen 18090;
    server_name yourdomain.com;
    location /file {
        alias /data/file/;
        autoindex on;  				            # 启用目录列表
        	autoindex_exact_size off;  	# 以 KB、MB、GB 显示文件大小
        	autoindex_localtime on;    		# 显示服务器本地时间而非 UTC 时间
    	}
}

有时也会使用 root 代替 alias 使用,使用 root 时,整个 url 会被追加到 root 指定的路径上,即实际文件目录为变为 /data/file/file/

2、文件访问

代理地址:http://192.168.1.59:8200/api/upload/2023/04/25/1.png

        location /api/upload/ {
            proxy_pass http://192.168.216.130:18083/uploadPath/upload/;
        }
3、邮件转发

内网邮件通过 Nginx 代理服务器进行转发。

image-20220325133718280

使用 stream 方式代理 smtp 协议的邮件服务,需安装 stream 模块

# 查看已安装模块
./nginx -V

# 编译时需加上 stream 模块
./configure --prefix=/data/nginx --with-stream ...
make && make install

Nginx 配置

stream {
    # 邮件转发
    server {
        listen 8765;
        proxy_connect_timeout 5s;
        proxy_timeout 5s;
        proxy_pass smtp.qq.com:587;
    }
}

Search

    Table of Contents