nginx 配置教程
基本概述
Nginx(发音为“engine-x”)是一款开源、高性能的HTTP服务器和反向代理服务器,由俄罗斯程序员Igor Sysoev于2004年开发 它以其高并发处理能力、低资源消耗和模块化设计闻名,广泛应用于全球顶级互联网公司(如百度、淘宝、腾讯等
nginx 安装与卸载
点击查看教程
安装 nginx
通过nginx搭建webdav服务需要安装http_dav_module模块。nginx-full版本中直接包含了http_dav_module模块。
1 、安装 nginx-full(webdav服务版)
1
| sudo apt update && sudo apt install nginx-full -y
|
2 、查看 nginx 版本
3 、启动 nginx 服务
4 、查看 nginx 状态
1
| sudo systemctl status nginx
|
5 、设置 nginx 开机自动启动
1
| sudo systemctl enable nginx
|
6 、升级 nginx-full
1
| sudo apt update && sudo apt upgrade nginx-full -y
|
nginx 维护命令
1 、测试配置文件
2 、重启nginx服务
1
| sudo systemctl restart nginx
|
3 、判断配置文件格式是否正确
4 、查看当前 Nginx 最终的配置
5 、重新加载配置文件
6 、检查 nginx 服务是否已启用(输出 enabled 表示服务已成功启用)
1
| sudo systemctl is-enabled nginx
|
7 、优雅停止Nginx
8 、强制停止Nginx
1
| sudo systemctl stop nginx
|
9 、重新加载 .bashrc 文件
10 、查看nginx日志
1 2 3
| cat /var/log/nginx/error.log
|
11 、测试特定的nginx配置文件是否正确
1
| nginx -t -c /etc/nginx/conf.d/xunlei.conf
|
12 、查看 80 端口占用情况
1
| netstat -tulnp | grep "0.0.0.0:80"
|
卸载 nginx
1 、卸载 nginx-full
1
| sudo apt remove nginx-full -y
|
2 、卸载 nginx 及其配置文件
1
| sudo apt remove --purge nginx-full -y
|
3 、卸载 nginx 删除所有自动生成的配置文件和日志文件
1
| sudo apt autoremove --purge nginx-full -y
|
nginx 反向代理
点击查看教程
xunlei 下载器 内网地址:http://localhost:2345
xunlei 下载器 公网地址:https://xunlei.example.com:666
创建 xunlei 反向代理配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| mkdir -pm 755 /etc/nginx/conf.d && touch /etc/nginx/conf.d/xunlei.conf && cat <<'EOF' | sed '1!{/^[[:space:]]*#/d;/^[[:space:]]*$/d}' > /etc/nginx/conf.d/xunlei.conf
server { listen 666 ssl; listen [::]:666 ssl;
server_name xunlei.example.com;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers HIGH:!aNULL:!MD5;
location / { proxy_pass http://localhost:2345; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Range $http_range; proxy_set_header If-Range $http_if_range; proxy_redirect off; proxy_buffering on; proxy_http_version 1.1; } charset utf-8; error_page 404 500 502 503 504 /50x.html; location = /50x.html { root /var/www/html; } } EOF
sudo systemctl restart nginx
|
nginx 负载均衡
点击查看教程
一 、nginx 热负备载均衡(我在使用的版本)
热备:如果你有2台服务器,当一台服务器发事生故时,才用启第二台务服器给供提服务。服器务处理请的求顺序:AAAAAA突然A挂啦,BBBBBBBBBBBBBB…
1 、创建 mtab 配置文件
mtab 书签页:https://mtab.example.com:666
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| mkdir -pm 755 /etc/nginx/conf.d && touch /etc/nginx/conf.d/mtab.conf && cat > /etc/nginx/conf.d/mtab.conf <<'EOF'
upstream mysvr { server localhost:9200; server localhost:9200 backup; }
server { listen 666 ssl; listen [::]:666 ssl;
server_name mtab.example.com;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem; location / { proxy_pass http://mysvr; add_header backendIP $upstream_addr; add_header backendCode $upstream_status; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Range $http_range; proxy_set_header If-Range $http_if_range; proxy_redirect off; proxy_buffering on; proxy_http_version 1.1; client_max_body_size 20000m; } } EOF
sudo systemctl restart nginx
|
2 、测试配置文件
3 、重启nginx服务
1
| sudo systemctl restart nginx
|
4 、查看nginx状态
1
| sudo systemctl status nginx
|
5 、查看负载均衡
访问:https://mtab.example.com:666
网页打开,开发者工具,选择网络,找到mtab.example.com,就可以查看到
二 、nginx 轮询负均责衡
- 轮询:nginx默认就轮是询其权重默都认为1,服务器处理求请的顺序:ABABABABAB…
1 2 3 4 5 6 7 8 9 10 11 12
| upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333; }
server { listen 8081; server_name 127.0.0.1; location /{ proxy_pass http://mysvr; } }
|
三 、nginx 加权轮询载负规则
加权轮询:跟配据置的权的重大小而分发给同不服务不器同数的量请求。如果不设置,则默认为1。下面服务器的求请顺序为:ABBABBABBABBABB…
1 2 3 4 5 6 7 8 9 10 11 12
| upstream mysvr { server 127.0.0.1:7878 weight=1; server 192.168.10.121:3333 weight=2; }
server { listen 8081; server_name 127.0.0.1; location /{ proxy_pass http://mysvr; } }
|
四 、 nginx ip_hash 负载均衡
ip_hash:nginx会让相同的户客端ip请求相同服的务器。
1 2 3 4 5 6 7 8 9 10 11 12 13
| upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333; ip_hash; }
server { listen 8081; server_name 127.0.0.1; location /{ proxy_pass http://mysvr; } }
|
五 、nginx 对定特资源实负现载均衡
通过多个upstream分成多服个务器组,将同不的请求分流不到同的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| upstream videomysvr { server 127.0.0.1:7878 weight=1; server 192.168.10.121:3333 weight=2; }
upstream filemysvr { server 192.3.2.1:7878 weight=1; server 192.3.2.2:3333 weight=2; }
server { listen 8081; server_name 127.0.0.1; location /video/ { proxy_pass http://videomysvr; } location /file/ { proxy_pass http://filemysvr; } }
|
六 、nginx 对不同域名现实负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| upstream videomysvr { server 127.0.0.1:7878 weight=1; server 192.168.10.121:3333 weight=2; }
upstream filemysvr { server 192.3.2.1:7878 weight=1; server 192.3.2.2:3333 weight=2; }
server { listen 8081; server_name www.ustc.edu.cn; location /video/ { proxy_pass http://videomysvr; } }
server { listen 8082; server_name www.ustc.sse.edu.cn; location /file/ { proxy_pass http://filemysvr; } }
|
七 、nginx 实现带有URL重的写负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| upstream backend{ server 192.168.200.146:9001; server 192.168.200.146:9002; server 192.168.200.146:9003; }
server { listen 80; server_name localhost; location /file/ { rewrite ^(/file/.*) /server/$1 last; } location / { proxy_pass http: } }
|
八 、nginx 四层负均载衡
首先安装好redis和tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| stream { upstream redisbackend { server 192.168.200.146:6379; server 192.168.200.146:6378; }
upstream tomcatbackend { server 192.168.200.146:8080; }
server { listen 81; proxy_pass redisbackend; } server { listen 82; proxy_pass tomcatbackend; } }
|
nginx 文件服务
点击查看教程
nginx 文件服务 公网地址:https://file.example.com:666
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| mkdir -pm 755 /etc/nginx/conf.d /mnt/file && touch /etc/nginx/conf.d/file.conf && cat <<'EOF' | sed '1!{/^[[:space:]]*#/d;/^[[:space:]]*$/d}' > /etc/nginx/conf.d/file.conf
server { listen 666 ssl;
server_name file.example.com;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers HIGH:!aNULL:!MD5;
location / { root /mnt/file; if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){ add_header Content-Disposition attachment; } autoindex on; sendfile on; autoindex_localtime on; autoindex_format html; autoindex_exact_size off; charset utf-8,gbk; } } EOF
sudo systemctl restart nginx
|
nginx 搭建 webdav 服务器
点击查看教程
部署 nginx webdav 服务器
1、安装 nginx-full
通过nginx搭建webdav服务需要安装http_dav_module模块。nginx-full版本中直接包含了http_dav_module模块。
1
| sudo apt update && sudo apt install nginx-full -y
|
2 、设置权限
1
| sudo chown -R www-data:www-data /mnt && sudo chmod -R 755 /mnt
|
3 、创建用户登录验证信息(用户名-密码,自行修改)
1 2
| rm /etc/webdav/.credentials.list && sudo mkdir -p /etc/webdav && touch /etc/webdav/.credentials.list && echo -n 'admin:' | sudo tee -a /etc/webdav/.credentials.list && sudo openssl passwd -apr1 123456 | sudo tee -a /etc/webdav/.credentials.list
|
4 、创建 webdav 配置文件
webdav 公网地址:https://webdav.example.com:666
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| mkdir -pm 755 /etc/nginx/conf.d && touch /etc/nginx/conf.d/webdav.conf && chmod u+x /etc/nginx/conf.d/webdav.conf && cat > /etc/nginx/conf.d/webdav.conf <<'EOF' server {
listen 666 ssl;
server_name webdav.example.com;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers HIGH:!aNULL:!MD5;
charset utf-8;
autoindex on;
root /mnt; auth_basic realm_name;
auth_basic_user_file /etc/webdav/.credentials.list;
dav_methods PUT DELETE MKCOL COPY MOVE; dav_ext_methods PROPFIND OPTIONS;
dav_access user:rw group:rw all:r;
client_body_temp_path /tmp;
client_max_body_size 0;
create_full_put_path on; } EOF
sudo systemctl restart nginx
|
nginx 维护命令
1 、查看nginx状态
1
| sudo systemctl status nginx
|
2 、升级 nginx-full
1
| sudo apt update && sudo apt upgrade nginx-full -y
|
手机 app 登陆(ES文件浏览器)

可以搭配frp等内网穿透工具将本地webdav服务暴露,这样就可以将本地主机当做网盘来使用。
手机客户端可使用ES文件浏览器,使用效果良好。
玩法挺多,慢慢探究
nginx 图床部署
点击查看教程
图床根目录地址:https://blog.example.com:666/
图床访问图片地址:https://blog.example.com:666/img/tmdb-01.jpg
必须匹配到图片文件才可以
访问根目录,显示nginx欢迎页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| mkdir -pm 755 /etc/nginx/conf.d /mnt/file/blog && touch /etc/nginx/conf.d/blog.conf && cat <<'EOF' | sed '1!{/^[[:space:]]*#/d;/^[[:space:]]*$/d}' > /etc/nginx/conf.d/blog.conf
server { listen 666 ssl; server_name blog.example.com.org;
ssl_certificate /etc/nginx/keyfile/cert.pem; ssl_certificate_key /etc/nginx/keyfile/key.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers HIGH:!aNULL:!MD5;
location ~ .*\.(gif|jpg|lrc|txt|doc|pdf|rar|gz|zip|jpeg|png|mp4|mkv|html|yaml)$ { root /mnt/file/blog; if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|lrc|docx|exe|xlsx|ppt|pptx|yaml)$){ add_header Content-Disposition attachment; } autoindex on; sendfile on; autoindex_localtime on; autoindex_format html; autoindex_exact_size off; charset utf-8,gbk; expires 24h; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 1280k; proxy_connect_timeout 900; proxy_send_timeout 900; proxy_read_timeout 900; proxy_buffer_size 40k; proxy_buffers 40 320k; proxy_busy_buffers_size 640k; proxy_temp_file_write_size 640k; }
location ~* ^/[^.]+$ { root /mnt/file/blog; add_header Content-Disposition "attachment; filename=$uri"; autoindex on; sendfile on; autoindex_localtime on; autoindex_format html; autoindex_exact_size off; charset utf-8,gbk; expires 24h; } } EOF
sudo systemctl restart nginx
|
修改 Nginx 默认端口
点击查看教程
修改 Nginx 主配置文件
1
| nano /etc/nginx/nginx.conf
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| http {
...
server { listen 8833; # 将监听端口从 80 修改为 8833 server_name _;
root /var/www/html; index index.html index.htm index.nginx-debian.html;
location / { try_files $uri $uri/ =404; } } }
|
修改 Nginx 子配置文件
1
| nano /etc/nginx/sites-available/default
|
1 2 3
| server { listen 8855 default_server; # 修改为非80端口 listen [::]:8855 default_server; # 修改为非80端口
|