一、启动、停止、重启

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

  1. # 指该服务进程用户身份
  2. user nginx;
  3. # 指定工作进程数量(一般对应CPU核数)
  4. # worker_processes auto;
  5. worker_processes 1;
  6. # 亲核(特定的CPU调度特定的进程 无需切换提高效率)
  7. # worker_cpu_affinity auto;
  8. worker_cpu_affinity 0001;
  9. # 进程最大打开文件数(连接,此处必须修改内核的限制也必须支持该数值,内核限制默认1024,最大50000)
  10. worker_rlimit_nofile 1024;
  11. # 指定警告及以上级别的信息 日志文件
  12. error_log /var/log/nginx/error.log warn;
  13. # 指定存在进程号 的文件
  14. pid /var/run/nginx.pid;
  15. events {
  16. # 进程最大连接数
  17. worker_connections 1024;
  18. # 事件并发机制(poll、select、epoll) 用epoll机制(linux下最佳方式)
  19. use epoll;
  20. # 防止惊群
  21. multi_accept on;
  22. }
  23. http {
  24. # 包含 文件属性 文件
  25. include /etc/nginx/mime.types;
  26. default_type application/octet-stream;
  27. # 指定日志信息格式
  28. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  29. '$status $body_bytes_sent "$http_referer" '
  30. '"$http_user_agent" "$http_x_forwarded_for"';
  31. # 使用“main”格式记录日志到指定文件
  32. access_log /var/log/nginx/access.log main;
  33. # 开启快速发送文件方式
  34. sendfile on;
  35. tcp_nopush on;
  36. # 连接超时事件
  37. keepalive_timeout 65;
  38. # 压缩(优化选项)
  39. gzip on;
  40. gzip_min_length 2K;
  41. gzip_buffers 4 16k;
  42. gzip_comp_level 5;
  43. gzip_types text/plain application/x-javascript text/css application/xml text/html;
  44. # 包含子配置文件
  45. include /etc/nginx/conf.d/*.conf;
  46. }

2. 子配置文件/etc/nginx/conf.d/upl.conf

  1. #虚拟主机
  2. server {
  3. #监听在哪个端口
  4. listen 80;
  5. # 虚拟主机名称(站点域名)
  6. server_name bbs.upl.com;
  7. #指定字符集
  8. charset utf-8;
  9. #指定访问日志信息文件
  10. access_log /var/log/nginx/upl.access.log main;
  11. #通用匹配项
  12. location / {
  13. #指定站点根目录<起始目录>
  14. root /var/www/upl.com;
  15. #指定自动索引页面
  16. index index.html index.htm;
  17. }
  18. #设置某个目录列出文件信息 提供下载
  19. #当用户访问bbs.upl.com/download/ 则匹配该项
  20. location = /download/ {
  21. root /var/www/upl.com;
  22. #列出该目录底下的所有文件<用于资源下载>
  23. autoindex on;
  24. autoindex_exact_size on;
  25. autoindex_format html;
  26. autoindex_localtime on;
  27. #账号授权控制
  28. auth_basic "This You Need Input Pas:";
  29. #指定授权用户密码文件 文件格式为 user:passwd 其中密码为密文密码
  30. #生成密码openssl passwd (注意新版本nginx只支持密文密码)
  31. auth_basic_user_file /var/www/upl.com/password;
  32. }
  33. #查看nginx服务状态 /NginxStatus并不时站点中的实际文件或目录只作为访问配置localtion
  34. location = /NginxStatus {
  35. stub_status on; ----开启状态查询
  36. access_log off; ----查询状态不写日志
  37. allow 192.168.64.1;
  38. deny all;
  39. }
  40. ##########状态信息#####################
  41. Active connections: 1 ---活跃的连接数
  42. server accepts handled requests
  43. 2 2 6 ----接受连接数 处理成功的连接数 接受请求数
  44. Reading: 0 Writing: 1 Waiting: 0 ---读取客户端的连接数 响应数据到客户端的数量 nginx已经处理完成正在等候下一次请求指令的数量
  45. ####################################
  46. #404 403错误配置项
  47. error_page 404 403 /404.html;
  48. location = /404.html {
  49. root /var/www/upl.com/error;
  50. }
  51. #500 502 503 504错误匹配项
  52. # redirect server error pages to the static page /50x.html
  53. #
  54. error_page 500 502 503 504 /50x.html;
  55. location = /50x.html {
  56. root /usr/share/nginx/html;
  57. }
  58. }

注意:

  • 只要修改过nginx 配置文件必须重启服务才能生效
  • systemctl restart nginx 或者 systemctl reload nginx
  • nginx -t 检查配置文件的语法

四、负载均衡

参考:使用Nginx作为HTTP负载均衡服务器

1. 负载均衡方法

nginx 支持以下负载均衡机制(或方法):

  • 轮询(**round-robin**) - 发送给应用服务器的请求以轮询的方式分发
  • 最少连接(**least-connected**) - 下一个请求被分配给具有最少数量活动连接的服务器
  • ip 哈希(**ip-hash**) - 使用哈希函数确定下一个请求应该选择哪一个服务器(基于客户端的 IP 地址)

2. 默认负载均衡配置

使用 nginx 进行负载均衡的最简单配置如下所示:

  1. http {
  2. upstream myapp1 {
  3. server srv1.example.com;
  4. server srv2.example.com;
  5. server srv3.example.com;
  6. }
  7. server {
  8. listen 80;
  9. location / {
  10. proxy_pass http://myapp1;
  11. }
  12. }
  13. }

3. 优化性配置

  1. upstream app {
  2. server 192.168.198.100:8000 weight=1 max_fails=3 fail_timeout=10s;
  3. server 192.168.198.101:8000 weight=1 max_fails=3 fail_timeout=10s;
  4. }
  5. server {
  6. listen 80;
  7. server_name www.app.com;
  8. charset utf-8;
  9. access_log /var/log/nginx/app.access.log main;
  10. location / {
  11. proxy_pass http://app;
  12. proxy_set_header X-Real-IP $remote_addr;
  13. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  14. proxy_redirect off;
  15. proxy_connect_timeout 30;
  16. proxy_send_timeout 15;
  17. proxy_read_timeout 15;
  18. }
  19. }

3. PHP负载均衡

  1. upstream phpServer {
  2. ip_hash;
  3. server 192.168.198.130:9000 max_fails=3 fail_timeout=10s;
  4. server 192.168.198.131:9000 max_fails=3 fail_timeout=10s;
  5. }
  6. #=== Fastcgi Config ===
  7. fastcgi_connect_timeout 30;
  8. fastcgi_send_timeout 30;
  9. fastcgi_read_timeout 30;
  10. fastcgi_buffer_size 64k;
  11. fastcgi_buffers 4 64k;
  12. fastcgi_busy_buffers_size 128k;
  13. fastcgi_temp_file_write_size 128k;
  14. location ~ \.(php|php5)$ {
  15. root /var/www/www.app.com;
  16. fastcgi_pass phpServer;
  17. fastcgi_index index.php;
  18. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  19. include fastcgi_params;
  20. }
  21. location = /php/ {
  22. root /var/www/www.app.com;
  23. fastcgi_pass phpServer;
  24. fastcgi_index index.php;
  25. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  26. include fastcgi_params;
  27. }