差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
itwiki:nginx-practice [2021/09/19 03:48] – [NGINX 实践] ovwx@live.io | itwiki:nginx-practice [2024/01/03 12:09] (当前版本) – [Nginx 实践] ovwx@live.io | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== | + | ====== |
> for Nginx v1.20.1 or Later | > for Nginx v1.20.1 or Later | ||
行 7: | 行 7: | ||
===== Nginx安装 ===== | ===== Nginx安装 ===== | ||
- | [[http:// | + | [[https:// |
我们可以将NGINX的软件源添加到系统中,然后通过系统自带的软件包管理系统,如YUM, | 我们可以将NGINX的软件源添加到系统中,然后通过系统自带的软件包管理系统,如YUM, | ||
行 15: | 行 15: | ||
rpm -qa | grep nginx | rpm -qa | grep nginx | ||
</ | </ | ||
+ | |||
+ | 以CentOS为例安装Nginx相关模块 | ||
+ | |||
+ | < | ||
+ | yum search nginx | ||
+ | yum install nginx-mod-ModName | ||
+ | </ | ||
+ | |||
===== 最小化 nginx 静态站配置 ===== | ===== 最小化 nginx 静态站配置 ===== | ||
行 36: | 行 44: | ||
- | ===== Nginx + 反向代理 + SSL ===== | ||
- | |||
- | <file site-xxx.conf> | ||
- | server { | ||
- | listen 80; | ||
- | listen [::]:80; | ||
- | server_name www.xxx.com; | ||
- | return 301 https:// | ||
- | } | ||
- | |||
- | server { | ||
- | listen | ||
- | listen | ||
- | server_name c.ezua.com; | ||
- | charset utf-8; | ||
- | |||
- | # ssl配置 | ||
- | ssl_protocols TLSv1.1 TLSv1.2; | ||
- | ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256: | ||
- | ssl_ecdh_curve secp384r1; | ||
- | ssl_prefer_server_ciphers on; | ||
- | ssl_session_cache shared: | ||
- | ssl_session_timeout 10m; | ||
- | ssl_session_tickets off; | ||
- | ssl_certificate / | ||
- | ssl_certificate_key / | ||
- | |||
- | root / | ||
- | location / { | ||
- | proxy_ssl_server_name on; | ||
- | proxy_pass https:// | ||
- | proxy_set_header Accept-Encoding ''; | ||
- | sub_filter " | ||
- | sub_filter_once off; | ||
- | } | ||
- | | ||
- | |||
- | location / | ||
- | proxy_redirect off; | ||
- | proxy_pass http:// | ||
- | proxy_http_version 1.1; | ||
- | proxy_set_header Upgrade $http_upgrade; | ||
- | proxy_set_header Connection " | ||
- | proxy_set_header Host $host; | ||
- | # 向后端传送真实IP地址 | ||
- | proxy_set_header X-Real-IP $remote_addr; | ||
- | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | ||
- | } | ||
- | } | ||
- | |||
- | </ | ||
- | |||
- | 其中: | ||
- | - 第一个 location 是通用路径反代,获取之后数据之后对部分内容进行替换。'' | ||
- | - 使用了高级功能的一些网站可能需要进行传递 Upgrade,以及 Connection 头部,如:WebSocket | ||
- | - X-Real-IP 多用于代理服务器,向真实服务器传递远程客户端IP地址 | ||
- | - Server Listen 80 端口,并使用301重定向 | ||
- | - X-Forwarded-For XFF头不是标准HTTP头部 | ||
- | - 第二个 location 可以设置的长一点,这样可以把该路径隐藏在转发的网站中 | ||
- | |||
- | ===== Nginx + PHP-fpm + SSL ===== | ||
- | |||
- | ==== 预装PHP及PHP-FPM环境 ==== | ||
- | |||
- | ==== 配置PHP fastcgi 转发 ==== | ||
- | |||
- | <file site-php-fpm.conf> | ||
- | |||
- | server { | ||
- | listen | ||
- | listen | ||
- | server_name www.c.ezua.com; | ||
- | charset utf-8; | ||
- | |||
- | # ssl配置 | ||
- | ssl_protocols TLSv1.1 TLSv1.2; | ||
- | ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256: | ||
- | ssl_ecdh_curve secp384r1; | ||
- | ssl_prefer_server_ciphers on; | ||
- | ssl_session_cache shared: | ||
- | ssl_session_timeout 10m; | ||
- | ssl_session_tickets off; | ||
- | ssl_certificate / | ||
- | ssl_certificate_key / | ||
- | |||
- | #root / | ||
- | root / | ||
- | index index.php; | ||
- | location ~ \.(php|php5).* { | ||
- | fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | ||
- | fastcgi_pass unix:/ | ||
- | include fastcgi_params; | ||
- | } | ||
- | |||
- | location / { | ||
- | |||
- | } | ||
- | |||
- | } | ||
- | |||
- | </ | ||
- | |||
- | 在配置方面与普通的反代没有区别,'' | ||
- | |||
- | ===== Nginx + 负载均衡 ===== | ||
- | |||
- | 暂未涉及 | ||
- | |||
- | ===== Nginx + 正向代理 ===== | ||
- | |||
- | 将 Nginx 作为一个HTTP代理服务器使用 | ||
- | |||
- | ==== 最小化配置 ==== | ||
- | |||
- | < | ||
- | |||
- | # Proxy-serv.conf | ||
- | |||
- | server { | ||
- | |||
- | listen 18081; | ||
- | server_name _; | ||
- | # other settings for example ssl | ||
- | | ||
- | # 代理CONNECT连接请求,代理443及503端口 | ||
- | proxy_connect; | ||
- | proxy_connect_allow 443 563; | ||
- | proxy_connect_connect_timeout 10s; | ||
- | proxy_connect_read_timeout 10s; | ||
- | proxy_connect_send_timeout 10s; | ||
- | | ||
- | location / { | ||
- | | ||
- | resolver 8.8.8.8; #DNS Server used | ||
- | proxy_pass $scheme:// | ||
- | proxy_set_header HOST $host; | ||
- | | ||
- | # | ||
- | # | ||
- | # | ||
- | | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | # | ||
- | | ||
- | } | ||
- | |||
- | error_page 500 502 503 504 /50x.html; | ||
- | | ||
- | location = /50x.html { | ||
- | root / | ||
- | } | ||
- | } | ||
- | </ | ||
- | |||
- | 注意:被注释掉的部分都是可以不需要的,是配置代理服务器的一般参数 | ||
- | |||
- | ==== 测试正向代理 ==== | ||
- | |||
- | 设置代理,如须在系统中一直启用,须在启动文件,诸如:'' | ||
- | < | ||
- | export http_proxy=$IP: | ||
- | export https_proxy=$IP: | ||
- | </ | ||
- | |||
- | 使用代理下载数据或者获取参数 | ||
- | < | ||
- | $ curl --proxy=$IP: | ||
- | </ | ||
- | 注:如果命令中包含引号,& | ||
===== Nginx location 及访问控制 ===== | ===== Nginx location 及访问控制 ===== | ||
行 259: | 行 94: | ||
# | # | ||
</ | </ | ||
+ | |||
+ | |||
+ | ===== 其他范例 ===== | ||
+ | |||
+ | * 用作代理服务器 [[itwiki: | ||
+ | * 搭建 dokuwiki [[itwiki: | ||
+ | * 用于支持PHP的配置 [[itwiki: | ||