1.守护进程 1 2 3 4 root@user-ubantu:~ root 6905 1 0 10:36 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; www-data 6907 6905 0 10:36 ? 00:00:00 nginx: worker process www-data 6908 6905 0 10:36 ? 00:00:00 nginx: worker process
2.查看端口占用情况 1 2 3 4 5 6 7 8 root@user-ubantu:~ COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 6905 root 6u IPv4 51224 0t0 TCP *:http (LISTEN) nginx 6905 root 7u IPv6 51225 0t0 TCP *:http (LISTEN) nginx 6907 www-data 6u IPv4 51224 0t0 TCP *:http (LISTEN) nginx 6907 www-data 7u IPv6 51225 0t0 TCP *:http (LISTEN) nginx 6908 www-data 6u IPv4 51224 0t0 TCP *:http (LISTEN) nginx 6908 www-data 7u IPv6 51225 0t0 TCP *:http (LISTEN)
3.命令 1 2 3 4 5 6 7 8 9 10 //优雅停止 $ nginx -s quit //立即停止 $ nginx -s stop //重载配置文件 $ nginx -s reload //重新打卡日志文件 $ nginx -s reopen //查看nginx 配置是否有误 $ nginx -t
1 2 3 root@gaomu-ubantu:/etc/nginx nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
1 2 3 4 5 root@gaomu-ubantu:~ nginx version: nginx/1.18.0 (Ubuntu) built with OpenSSL 3.0.2 15 Mar 2022 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -ffile-prefix-map=/build/nginx-zctdR4/nginx-1.18.0=. -flto=auto -ffat-lto-objects -flto=auto -ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -flto=auto -ffat-lto-objects -flto=auto -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-compat --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --add-dynamic-module=/build/nginx-zctdR4/nginx-1.18.0/debian/modules/http-geoip2 --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module
4.默认页面
nginx默认页面配置在 /etc/nginx/sites-enabled/default文件中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 server { listen 80 default_server; listen [::]:80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name _; location / { try_files $uri $uri / =404 ; } }
其中默认WEB页面路径 /var/www/html;
1 2 3 4 root@gaomu-ubantu:/var/www/html /var/www/html root@gaomu-ubantu:/var/www/html index.nginx-debian.html
5.修改work进程的数量
通常work进程的数量与内核数量保持一致即可
修改
将配置文件中的worker_processes auto;
auto值改为自己想要该的worker进程数,然后重载配置即可
6.配置文件详解
nginx配置文件主要分为三块
全局块
events块:网络连接的配置
http块
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 root@gaomu-ubantu:/etc/nginx user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf ;events { worker_connections 768 ; } http { sendfile on ; tcp_nopush on ; types_hash_max_size 2048 ; include /etc/nginx/mime.types; default_type application/octet-stream; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3 ; ssl_prefer_server_ciphers on ; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error .log; gzip on ; include /etc/nginx/conf.d/*.conf ; include /etc/nginx/sites-enabled/*; }
7.负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 upstream backend { ip_hash; server 127.0.0.1:8000 weight=3 ; server 127.0.0.1:8001 ; server 127.0.0.1:8002 ; } server { listen 80 ; server_name localhost; location /app { proxy_pass http://backend; } }
8.反向代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 server { listen 80 ; listen [::]:80 ; server_name example.com; root /var/www/example.com; index index.html; location / { try_files $uri $uri / =404 ; proxy_pass http://127.0.0.1:8000/; } }
location和proxy_pass都末尾都加路径
1 2 3 4 5 location /python/ { try_files $uri $uri / =404 ; proxy_pass http://192.168.31.173:5000/; }
都不加路径
1 2 3 4 5 location /python { try_files $uri $uri / =404 ; proxy_pass http://192.168.31.173:5000; }
注意:从外网访问使用域名,一般通过conf.d目录创建配置文件进行添加配置,如果是本地反代,一般通过/etc/nginx/sites-enabled/default 文件进行配置
9.HTTPS协议
1 2 3 4 5 6 $ openssl genrsa -out private.key 2048 $ openssl req -new key private.key -out cert.csr $ openssl x509 -req -in cert.csr -out cacert.pem -signkey private.key
1 2 3 4 5 $ openssl genpkey -algorithm RSA -out key.pem -pkeyopt rsa_keygen_bits:2048 $ openssl req -new -x509 -key key.pem -out cacert.pem -days 365
1 2 3 4 5 6 7 8 9 listen 443 ssl default_server;listen [::]:443 ssl default_server;ssl_certificate /home/openssl/cacert.pem;ssl_certificate_key /home/openssl/key.pem;
以上方法只能获得自签名,证书可以使用certbot获得签名证书
1 2 3 4 5 6 7 8 $ sudo snap install --classic certbot $ sudo ln -s /snap/bin/certbot /usr/bin/certbot $ sudo certbot --nginx $ sudo service nginx restart