Nginx 安装、配置文档 开始之前 本文档基于CentOS Linux 7 (Core)编写
编译安装 nginx-1.20.1
的源码包均存放至 /usr/local/src/
目录下
路径参数
根据实际使用需要修改
释义
参数名
本文配置
备注
服务目录
–prefix
/usr/local/nginx
NGINX 可执行文件的位置与名字
–sbin-path
/usr/sbin/nginx
非必须,默认为[prefix]/sbin/nginx
存放主进程PID文件位置
–pid-path
/usr/local/nginx/nginx.pid
非必须,默认为[prefix]/logs/nginx.pid
错误日志位置
–error-log-path
/var/log/nginx/error.log
非必须,默认为[prefix]/logs/error.log
访问日志位置
–http-log-path
/var/log/nginx/access.log
非必须,默认为[prefix]/logs/access.log
1 2 3 4 5 6 7 8 9 10 11 12 13 tar -xvf /usr/local/src/nginx-1.20.1.tar.gzcd /usr/local/src/nginx-1.20.1 ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --pid-path=/usr/local/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module make && make installln -s /usr/sbin/nginx /bin/nginx
测试安装 1 2 3 4 5 nginx -t $ nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok $ nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
配置Nginx 目录准备 1 2 3 4 5 6 mkdir /usr/local/nginx/vhost -pmkdir /usr/local/nginx/cert -pmkdir /usr/local/web -p
主配置修改 1 2 vim /usr/local/nginx/conf/nginx.conf
无备注版配置文件详见同级目录
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 worker_processes 4 ;error_log /var/log/nginx/error .log;pid /usr/local/nginx/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 /var/log/nginx/access.log main; proxy_set_header Host $host ; proxy_set_header Cookie $http_cookie ; proxy_set_header X-Forwarded-For $remote_addr ; sendfile on ; tcp_nopush on ; tcp_nodelay on ; keepalive_timeout 300s 300s ; keepalive_requests 10000 ; client_body_timeout 20s ; client_header_timeout 10s ; send_timeout 120s ; limit_conn_zone $binary_remote_addr zone=addr:10m ; limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s; limit_conn perip 20 ; limit_rate 10m ; gzip on ; add_header X-Frame-Options SAMEORIGIN; autoindex off ; include /usr/local/nginx/tools/error Page.conf; include /usr/local/nginx/vhost/*; }
站点配置模板 此处以项目名为web-manager
,域名为admin.aeroz.cn
作为案例
在此之前
将构建后的前端页面放入/usr/local/web/web-manager目录下
HTTP 1 2 3 4 cd /usr/local/nginx/vhost vi web-manager.conf
web-manager.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 server { listen 80 ; listen [::]:80 ; server_name admin.aeroz.cn; root /usr/local/web/web-manager; index index.html; try_files $uri $uri / /index.html; location /info { alias /usr/local/web/web-manager/info ; index info .html; } location /api { proxy_pass http://127.0.0.1:8080; client_max_body_size 512m ; } }
启动/重载/停止 1 2 3 4 5 6 7 8 9 10 nginx -s start nginx -t nginx -s reload nginx -s stop
将Nginx注册为系统服务 1 vi /lib/systemd/system/nginx.service
nginx.service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 [Unit] Description =The NGINX HTTP and reverse proxy server After =syslog.target network-online.target remote-fs.target nss-lookup.target Wants =network-online.target [Service] Type =forking PIDFile =/usr/local/nginx/nginx.pid ExecStartPre =/usr/sbin/nginx -t ExecStart =/usr/sbin/nginx ExecReload =/usr/sbin/nginx -s reload ExecStop =/bin/kill -s QUIT $MAINPID PrivateTmp =true [Install] WantedBy =multi-user.target
1 2 3 4 5 6 7 8 9 10 systemctl daemon-reload systemctl enable nginx.service systemctl enable nginx systemctl disable nginx.service systemctl disable nginx