Nginx(一) —— 简介、安装、部署
简介
安装
安装就不多写了,使用不同平台的安装命令轻松的安装
CentOS
# yum install nginx
Ubuntu
# sudo apt-get install nginx
Mac
# brew install nginx;
上述的方式安装,配置文件在 /etc/nginx/nginx.conf
# 查看一下配置文件
# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
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 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
# 这里是我Blug的配置,将api接口转向Tomcat
location /blog/api/ {
proxy_pass http://localhost:8080/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 其他的请求转向nodejs服务
location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
Nginx命令
验证配置是否正确
# nginx -t
查看Nginx的版本号
# nginx -V
启动Nginx
# start nginx
快速停止或关闭Nginx
# nginx -s stop
正常停止或关闭Nginx
# nginx -s quit
配置文件修改重装载命令
# nginx -s reload
通过系统命令查看nginx的状态
# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
# 这里就是运行状态,如果是dead表示挂了
Active: active (running) since Fri 2018-06-15 14:26:58 CST; 5 days ago
Main PID: 22209 (nginx)
CGroup: /system.slice/nginx.service
├─22209 nginx: master process /usr/sbin/nginx
└─29432 nginx: worker process
Jun 15 14:26:58 iZbp14xftdgx0l7g5jy51mZ systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jun 15 14:26:58 iZbp14xftdgx0l7g5jy51mZ nginx[22203]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jun 15 14:26:58 iZbp14xftdgx0l7g5jy51mZ nginx[22203]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jun 15 14:26:58 iZbp14xftdgx0l7g5jy51mZ systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Jun 15 14:26:58 iZbp14xftdgx0l7g5jy51mZ systemd[1]: Started The nginx HTTP and reverse proxy server.
重启报错
在重启nginx时会经常以下错
nginx: [error] CreateFile() "E:\nginx\nginx-1.9.3/logs/nginx.pid" failed
nginx: [error] Open() "E:\nginx\nginx-1.9.3/logs/nginx.pid" failed
这是因为nginx在停止时,nginx.pid被删除了。而reload命令需要通过nginx.pid获取进程号,如果不存在,就报错了。这时你可以用service命令来重启nginx服务
# service start nginx
大概在安装部署的步骤和遇到的问题就这么多了,下一篇,整理一下nginx的相关配置;
Nginx(二) —— 配置
上一篇中记录整理了一些nginx简介和安装、部署、命令的基本内容,本篇整理一些反向代理,https server, 负载均衡的配置,当然nginx作为一款高性能的web服务器,功能远不止这些,如果还想深入,一起来学习《Nginx 高性能Web服务器详解 —— 苗泽编著》这本书吧;
nginx的配置分为以下几个块,每个块都有一些相应的配置,配置以及详解
推荐阅读张龙豪的博客——Nginx配置详解,这里只对我工作中用到的进行一下整理总结
... #全局块
events { #events块
...
}
http { #http块
... #http全局块
server { #server块
... #server全局块
location [PATTERN] { #location块
...
}
location [PATTERN]
{
...
}
}
server {
...
}
... #http全局块
}
反向代理
在server块中主要配置两个内容,一个是listen监听端口,server_name监听地址
# 表示该虚拟主机监听 127.0.0.1 的 8080 端口,对此server块中的location进行匹配执行相应的处理
server {
listen 8080;
server_name 127.0.0.1;
location / {
...
}
}
通过过server块监听ip:port,就将请求在此server块中的location进行匹配执行相应的处理
# 这里是我Blug的配置,将api接口转向Tomcat,从而提供数据服务
location /blog/api/ {
# 代理转发的地址,比如将 ip:port/blog/api/article/list 转发代理到 http://localhost:8080/api/
proxy_pass http://localhost:8080/api/;
# 这里的proxy_set_header 配置是为了在传递客户端的真实ip:port,以便记录在日志当中,否则获取到的就是虚拟主机的ip和端口
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 其他的请求转向nodejs(controller和view),装配页面返回给前端
location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 当然location也支持正则匹配,匹配以.php结尾的URL
location \.php$ {
# 拒绝的ip
deny 127.0.0.1;
# 允许的ip
allow 172.18.5.54;
}
HTTPS server
网站采用的是HTTPS协议,在nginx中配置server块
server {
listen 433 ssl http2 default_server;
listen [::]:433 ssl http2 default_server;
# 证书路径;
ssl_certificate "xxx.crt";
# key路径;
ssl_certificate_key "xxx.key";
# 会话的缓存类型和大小
ssl_session_cache shared:SSL:1m;
# 会话过期时间
ssl_session_timeout 5m;
# 为建立安全连接,服务器所允许的密码格式列表
ssl_ciphers HIGH:!aNULL:!MD5;
#依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location / {
...
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
负载均衡
nginx的另一大功能就是负载均衡,通过负载均衡,将来自外部访问或数据流量分发到多个内部服务器的节点上分别处理,有效的减少前端的等待相应时间。
# 全局块
# 后端服务器组
upstream services {
server 192.168.5.2:80;
server 192.168.5.3:80;
server 192.168.5.4:80;
...
}
server {
listen 80;
server_name www.nicolasguo.com;
location / {
# 这里的services 为全局块中配置的
proxy_pass http://services
}
}
上面的权值都默认是1,当然也可以配置对所有请求实现加权轮询规则的负载均衡
# 全局块
# 后端服务器组
upstream services {
server 192.168.5.2:80 weight=5;
server 192.168.5.3:80 weight=3;
server 192.168.5.4:80;
...
}
server {
...
location / {
...
}
}
我在学习工作中用到也就这些了,供自己和新手看看吧