cenots7.4 源码 1.21.6

yum -y install pcre-devel openssl-devel
useradd -r -u 80 www
cat > /etc/security/limits.d/nginx.conf << end
www soft nofile 65535
www hard nofile 65535
end
ulimit -n 65535

tar xf nginx-1.21.6.tar.gz
cd nginx-1.21.6/
./configure —prefix=/apps/nginx-1.21.6 —user=www —group=www —with-http_stub_status_module —with-http_ssl_module —with-http_gzip_static_module —with-http_realip_module —with-pcre —with-openssl= —with-stream

make && make install

cd /apps
ln -s nginx-1.21.6 nginx

echo ‘export PATH=$PATH:/apps/nginx/sbin’ > /etc/profile.d/nginx.sh
. /etc/profile.d/nginx.sh

nginx.conf 示例

  1. user www www;
  2. worker_processes auto;
  3. pid /data/nginx/logs/nginx.pid;
  4. error_log /data/nginx/logs/nginx_error.log crit;
  5. #Specifies the value for maximum file descriptors that can be opened by this process.
  6. worker_rlimit_nofile 65535;
  7. events
  8. {
  9. use epoll;
  10. worker_connections 65535;
  11. }
  12. http
  13. {
  14. # server_tokens off;
  15. include mime.types;
  16. default_type application/octet-stream;
  17. log_format main
  18. '$remote_addr - $remote_user [$time_local] "$request" '
  19. '$status $body_bytes_sent "$http_referer" '
  20. '"$http_user_agent" "$http_x_forwarded_for"';
  21. # access_log /var/log/nginx/ngnix-access.log main;
  22. # error_log /var/log/nginx/ngnix-error.log;
  23. #charset gb2312;
  24. server_names_hash_bucket_size 128;
  25. client_header_buffer_size 32k;
  26. large_client_header_buffers 4 32k;
  27. client_max_body_size 8m;
  28. keepalive_timeout 60;
  29. sendfile on;
  30. tcp_nopush on;
  31. tcp_nodelay on;
  32. # FastCGI Configure
  33. fastcgi_connect_timeout 300;
  34. fastcgi_send_timeout 300;
  35. fastcgi_read_timeout 300;
  36. fastcgi_buffer_size 64k;
  37. fastcgi_buffers 4 64k;
  38. fastcgi_busy_buffers_size 128k;
  39. fastcgi_temp_file_write_size 128k;
  40. # gzip compression configure
  41. gzip on;
  42. gzip_http_version 1.0;
  43. gzip_comp_level 2;
  44. # gzip_proxied any; // Nginx Proxy use
  45. gzip_proxied any;
  46. gzip_min_length 1k;
  47. gzip_buffers 4 16k;
  48. gzip_types text/plain application/x-javascript text/css application/xml;
  49. gzip_vary on;
  50. # limit_zone one $binary_remote_addr 10m;
  51. server {
  52. listen 80 default_server;
  53. server_name default;
  54. #return 401;
  55. root /data/nginx/webroot/default;
  56. index index.html index.php;
  57. access_log /data/nginx/logs/default.acc;
  58. error_log /data/nginx/logs/default.err;
  59. location ~ \.php$ {
  60. fastcgi_pass unix:/tmp/fpm.sock;
  61. #fastcgi_pass 127.0.0.1:9000;
  62. fastcgi_index index.php;
  63. fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  64. include fastcgi.conf;
  65. }
  66. }
  67. include vhosts/*.conf;
  68. }

vim nginx.conf 语法高亮

cp -r /data/src/nginx-1.21.6/contrib/vim/ ~/.vim
image.png

服务启停方式

信号控制

kill -XXX pid

TERM/INT 关闭
QUIT 优雅关闭,包含 master 进程
HUP 重读配置文件并使用服务对新配置项生效
USR1 重新打开日志文件
USR2 平滑升级到最新版nginx
保留之前的 master 及 worker,原 pid文件 改为 pid.oldbin
新创建一组 master 及 worker ,master pid记录到 新pid文件

WINCH 子进程不再接收新连接,相当于对 worker 进程发送 QUIT ,保留 master 进程

命令控制

nginx -s XXX

stop 关闭
quit 优雅关闭
reopen 重新打开日志文件
reload 重读配置文件并使用服务对新配置项生效

nginx systemctl

/usr/lib/systemd/system/nginx.service

[Unit]
Description=web nginx
After=network.target

[Service]
Type=forking
ExecStart=/apps/nginx/sbin/nginx
ExecReload=/apps/nginx/sbin/nginx -s reload
ExecStop=/apps/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=default.target

windows

借助”Windows Service Wrapper”小工具,winsw-1.18-bin.exe
放在 nginx安装目录下,并重命名为 service.exe
创建配置文件 service.xml(名字要和工具名一样)

nginx
nginx
nginx
F:\nginx-log

10240
8

F:\nginx-1.18.0\nginx.exe
F:\nginx-1.18.0\nginx.exe -s stop

cmd> ./service.exe install