Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。它也是一种轻量级的Web服务器,可以作为独立的服务器部署网站(类似Tomcat)。它高性能和低消耗内存的结构受到很多大公司青睐,如淘宝网站架设。
nginx服务器
windows下安装nginx
下载地址:nginx官网
下载之后,解压到指定的目录,就可以看到以下的目录
D:\Program Files\nginx-1.8.1>start nginx
D:\Program Files\nginx-1.8.1>
nginx命令
start nginx 开启nginx服务
nginx.exe -s stop 关闭nginx服务,快速停止nginx,可能并不保存相关信息
nginx.exe -s quit 关闭nginx服务,完整有序的停止nginx,并保存相关信息
nginx.exe -s reload 重载nginx服务,当你改变了nginx配置信息并需要重新载入这些配置时可以使用此命令重载nginx
nginx的配置文件是conf目录下的nginx.conf,默认配置的nginx监听的端口为80
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
当我们修改了nginx的配置文件nginx.conf 时,不需要关闭nginx后重新启动nginx,只需要执行命令 nginx -s reload 即可让改动生效
在Nginx服务器上安装证书
在Nginx独立服务器上安装证书
下载证书,存放证书
配置nginx.conf
server {
listen 443 ssl;
server_name m.yipinxieli.com;
ssl_certificate cert/m.yipinxieli.com.pem;
ssl_certificate_key cert/m.yipinxieli.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
设置HTTP请求自动跳转HTTPS
Nginx 老版本的写法
server {
listen 80;
server_name www.yipinxieli.com; #需要将yourdomain替换成证书绑定的域名。
rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
location / {
index index.html index.htm;
}
}
新版本推荐
return 301 https://$server_name$request_uri; #http跳转https
Nginx负载均衡
#设定负载均衡的服务器列表
upstream load_balance_server {
#weigth参数表示权值,权值越高被分配到的几率越大
server 172.20.241.141:80 weight=5;
server 172.20.241.151:80 weight=1;
}
location / {
proxy_pass http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表
}
负载均衡中的健康检查:http://t.zoukankan.com/larry-luo-p-10620263.html
正向代理与反向代理
正向代理
对目标服务器隐藏客户端(用户)。客户端(用户)知道目标服务器(github),但是访问不到,可以通过代理(VPN)访问目标服务器,目标服务器只知道代理访问了自己,不知道客户端是谁。
反向代理
对客户端(用户)隐藏目标服务器。客户端只知道代理,不知道目标服务器,但通过代理客户端实际访问的是目标服务器,目标服务器知道客户端是谁。
最近用 Nginx 反向代理的几个网站经常出打不开,报 502 或 504 错误,重启 Nginx 后马上就好了。
实践问题:
Nginx 反向代理的动态域名 IP 随时会变化(宽带重新拨号),但 IP 改变后 Nginx 的反向代理还是用的缓存的旧 IP。
https://www.nadeau.io/post/nginx-proxy_pass-dns-cache/
location = / {
resolver 10.128.232.53 10.128.232.58 valid=5 ipv6=off;
set $cdnurl http://feweb.58dns.org/pro/html/authority-web;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass $cdnurl;
proxy_intercept_errors on;
}
引用