安装 下载地址: https://nginx.org/en/download.html
源码编译安装 编译安装nginx 使其支持tcp代理
如果系统已经通过yum
等工具直接安装了nginx,也不妨碍使用源码重新编译一份。注意安装位置不要冲突即可,也可以写在通过工具安装的 nginx 版本
1 2 ./configure --prefix=/your/path --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_preread_module make && make install
Yum 安装补全 nginx 安装所有模块,开启 tcp 反向代理。
解决 nginx: [emerg] unknown directive “stream“ in /etc/nginx/nginx.conf 问题。
1 2 3 4 5 curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo yum -y install epel-release yum -y install nginx-all-modules.noarch
指定配置文件 使用-c
参数指定配置文件
1 nginx -c conf/nginx.conf
包含配置文件夹 在conf
主配置文件中http
块中添加下面的参数,默认应该已经存在。
1 include /your/path/conf.d/*.conf;
默认使用配置文件示例
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 user root; worker_processes auto;error_log /var/log/nginx/error .log;pid /run/nginx.pid;include /usr/share/nginx/modules/*.conf ;events { worker_connections 1024 ; } http { log_format main '$remote_addr - $remote_user [$time_local ] "$request " ' '$status $body_bytes_sent "$http_referer " ' '"$http_user_agent " "$http_x_forwarded_for "' ; access_log /var/log/nginx/access.log main; sendfile on ; tcp_nopush on ; tcp_nodelay on ; keepalive_timeout 65 ; types_hash_max_size 4096 ; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf ; server { listen 80 ; listen [::]:80 ; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf ; error_page 404 /404 .html; location = /404 .html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
TCP 代理 ⚠️注意 :该配置需要放在主配置文件中 http
块同级。
可以在主配置文件中使用 include
来引入独立出去的配置文件。
stream 块中的 server 无法和 http 中的 server 共用监听同一个端口。否则启动时 nginx 会报错,提示端口被占用,无法启动。但是,注释掉 stream 相关配置,启动 nginx 后,取消 stream 的注释,使用 nginx -s reload
可以使配置生效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 stream { map $ssl_preread_server_name $host_name { mc.chilisdy.site mc; } upstream mc { server 127.0.0.1:25565 ; } server { listen 80 ; proxy_pass $host_name ; ssl_preread on ; } } http { .... }
HTTP 参考下面的配置,禁止IP访问等配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 server { listen 80 default; server_name _; return 403 ; } server { listen 443 default; server_name _; return 403 ; } server { listen 80 ; listen [::]:80 ; server_name demo.site www.demo.site; root /root/blog; access_log /var/log/nginx/blog.log; }
HTTPS 示例配置的证书是使用 Let's Encrypt
申请。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 server { listen 80 ; server_name chilisdy.site www.chilisdy.site; include /root/nginx/snippets/letsencrypt.conf; location / { return 308 https://www.chilisdy.site$request_uri ; } } server { server_name www.chilisdy.site; listen 443 ssl; listen [::]:443 ssl; ssl_certificate /etc/letsencrypt/live/www.chilisdy.site/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.chilisdy.site/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/www.chilisdy.site/fullchain.pem; include /root/nginx/snippets/ssl.conf; root /root/blog; access_log /var/log/nginx/blog_https.log; }
ssl.conf
配置内容如下,都是 ssl 的常规配置。多个网站可以共用改配置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ssl_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; ssl_protocols TLSv1.2; ssl_ciphers EECDH+AESGCM:EECDH+AES; ssl_ecdh_curve secp384r1; ssl_prefer_server_ciphers on; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload"; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff;
HTTP 代理 1 2 3 4 5 6 7 8 server { listen 80 ; server_name tool.demo.site; location / { proxy_pass http://192.15.55.23:9887; } }
配置多个 Location 配置多个 Location 可以实现同域名,不同前缀访问不同的网站。
主要区分好 alias 和 root 的区别就可以。但是这俩的作用都是一样的,指向实际资源存放的位置。
有以下网站配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 server { listen 8100 ; listen [::]:8100 ; location / { root /root/site/a; index index.html index.htm; } location /test_b { alias /root/site/b/; index index.html index.htm; } location /test_a { alias /root/site/a/; index index.html index.htm; } access_log /opt/log/nginx_daping.log; }
比如我们访问的网址是:http://127.0.0.1:8100/abc/123.txt
那么按照上面的配置,我们应该匹配第一个 /
路由配置,实际访问的文件路径是:/root/site/a/abc/123.txt
如果访问的网址是:http://127.0.0.1:8100/test_a/abc/123.txt
那么按照上面的配置,我们应该匹配的是第三个 /test_a
路由配置,实际访问的文件路径是: /root/site/a/abc/123.txt
同理如果访问 /test_b
那么实际访问的文件路径是: /root/site/b/abc/123.txt
这样对比 root 和 alias 的区别就很明显了:
alias 方式会以连接中同名的部分切分,后面的路径拼接到 alias 实际指向的位置
root 方式则是直接把端口后面的路径拼接到 root 实际指向的位置
区别总结:
alias 和 root 会影响 location 后面的 url 意义,如上测试。
alias 只能存在于 location 中,但是 root 可以存在 server、http 和 location 中。
alias 指定的路径必须以 /
结尾,root 则是可选
文件资源下载配置 1 2 3 4 5 6 7 8 9 10 server { listen 80 ; server_name dl.demo.site; charset utf-8 ; root /root/demo_server/upload; location / { default_type 'application/octet-stream' ; add_header Content-disposition "attachment" ; } }