一、启动、停止、重启
1. 启动
systemctl start nginx
2. 重启
systemctl restart nginx
3. 停止
systemctl stop nginx
二、查看Nginx状态
1. 服务状态
systemctl status nginx
3. 监听状态
netstat -nplt
三、nginx虚拟主机(站点)配置
1. 主配置文件/etc/nginx/nginx.conf
# 指该服务进程用户身份
user nginx;
# 指定工作进程数量(一般对应CPU核数)
# worker_processes auto;
worker_processes 1;
# 亲核(特定的CPU调度特定的进程 无需切换提高效率)
# worker_cpu_affinity auto;
worker_cpu_affinity 0001;
# 进程最大打开文件数(连接,此处必须修改内核的限制也必须支持该数值,内核限制默认1024,最大50000)
worker_rlimit_nofile 1024;
# 指定警告及以上级别的信息 日志文件
error_log /var/log/nginx/error.log warn;
# 指定存在进程号 的文件
pid /var/run/nginx.pid;
events {
# 进程最大连接数
worker_connections 1024;
# 事件并发机制(poll、select、epoll) 用epoll机制(linux下最佳方式)
use epoll;
# 防止惊群
multi_accept on;
}
http {
# 包含 文件属性 文件
include /etc/nginx/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"';
# 使用“main”格式记录日志到指定文件
access_log /var/log/nginx/access.log main;
# 开启快速发送文件方式
sendfile on;
tcp_nopush on;
# 连接超时事件
keepalive_timeout 65;
# 压缩(优化选项)
gzip on;
gzip_min_length 2K;
gzip_buffers 4 16k;
gzip_comp_level 5;
gzip_types text/plain application/x-javascript text/css application/xml text/html;
# 包含子配置文件
include /etc/nginx/conf.d/*.conf;
}
2. 子配置文件/etc/nginx/conf.d/upl.conf
#虚拟主机
server {
#监听在哪个端口
listen 80;
# 虚拟主机名称(站点域名)
server_name bbs.upl.com;
#指定字符集
charset utf-8;
#指定访问日志信息文件
access_log /var/log/nginx/upl.access.log main;
#通用匹配项
location / {
#指定站点根目录<起始目录>
root /var/www/upl.com;
#指定自动索引页面
index index.html index.htm;
}
#设置某个目录列出文件信息 提供下载
#当用户访问bbs.upl.com/download/ 则匹配该项
location = /download/ {
root /var/www/upl.com;
#列出该目录底下的所有文件<用于资源下载>
autoindex on;
autoindex_exact_size on;
autoindex_format html;
autoindex_localtime on;
#账号授权控制
auth_basic "This You Need Input Pas:";
#指定授权用户密码文件 文件格式为 user:passwd 其中密码为密文密码
#生成密码openssl passwd (注意新版本nginx只支持密文密码)
auth_basic_user_file /var/www/upl.com/password;
}
#查看nginx服务状态 /NginxStatus并不时站点中的实际文件或目录只作为访问配置localtion
location = /NginxStatus {
stub_status on; ----开启状态查询
access_log off; ----查询状态不写日志
allow 192.168.64.1;
deny all;
}
##########状态信息#####################
Active connections: 1 ---活跃的连接数
server accepts handled requests
2 2 6 ----接受连接数 处理成功的连接数 接受请求数
Reading: 0 Writing: 1 Waiting: 0 ---读取客户端的连接数 响应数据到客户端的数量 nginx已经处理完成正在等候下一次请求指令的数量
####################################
#404 403错误配置项
error_page 404 403 /404.html;
location = /404.html {
root /var/www/upl.com/error;
}
#500 502 503 504错误匹配项
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
注意:
- 只要修改过nginx 配置文件必须重启服务才能生效
- systemctl restart nginx 或者 systemctl reload nginx
- nginx -t 检查配置文件的语法
四、负载均衡
1. 负载均衡方法
nginx 支持以下负载均衡机制(或方法):
- 轮询(**round-robin**) - 发送给应用服务器的请求以轮询的方式分发
- 最少连接(**least-connected**) - 下一个请求被分配给具有最少数量活动连接的服务器
- ip 哈希(**ip-hash**) - 使用哈希函数确定下一个请求应该选择哪一个服务器(基于客户端的 IP 地址)
2. 默认负载均衡配置
使用 nginx 进行负载均衡的最简单配置如下所示:
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
3. 优化性配置
upstream app {
server 192.168.198.100:8000 weight=1 max_fails=3 fail_timeout=10s;
server 192.168.198.101:8000 weight=1 max_fails=3 fail_timeout=10s;
}
server {
listen 80;
server_name www.app.com;
charset utf-8;
access_log /var/log/nginx/app.access.log main;
location / {
proxy_pass http://app;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_connect_timeout 30;
proxy_send_timeout 15;
proxy_read_timeout 15;
}
}
3. PHP负载均衡
upstream phpServer {
ip_hash;
server 192.168.198.130:9000 max_fails=3 fail_timeout=10s;
server 192.168.198.131:9000 max_fails=3 fail_timeout=10s;
}
#=== Fastcgi Config ===
fastcgi_connect_timeout 30;
fastcgi_send_timeout 30;
fastcgi_read_timeout 30;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
location ~ \.(php|php5)$ {
root /var/www/www.app.com;
fastcgi_pass phpServer;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location = /php/ {
root /var/www/www.app.com;
fastcgi_pass phpServer;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}