nginx笔记
配置文件结构
实战演示
[root@localhost ~]# /opt/nginx/sbin/nginx
[root@localhost ~]#
[root@localhost ~]# ps -ef | grep nginx
root 1599 1 0 06:31 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx 1600 1599 0 06:31 ? 00:00:00 nginx: worker process
root 1603 1573 0 06:32 pts/0 00:00:00 grep --color=auto nginx
[root@localhost ~]#
[root@localhost ~]# cd /opt/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
[root@localhost conf]# cp nginx.conf nginx.conf.bak
[root@localhost conf]# ll
total 72
-rw-r--r--. 1 root root 1077 Apr 7 22:33 fastcgi.conf
-rw-r--r--. 1 root root 1077 Apr 7 22:33 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 Apr 7 22:33 fastcgi_params
-rw-r--r--. 1 root root 1007 Apr 7 22:33 fastcgi_params.default
-rw-r--r--. 1 root root 2837 Apr 7 22:33 koi-utf
-rw-r--r--. 1 root root 2223 Apr 7 22:33 koi-win
-rw-r--r--. 1 root root 5231 Apr 7 22:33 mime.types
-rw-r--r--. 1 root root 5231 Apr 7 22:33 mime.types.default
-rw-r--r--. 1 root root 2654 Apr 7 22:39 nginx.conf
-rw-r--r--. 1 root root 2654 Apr 9 06:33 nginx.conf.bak
-rw-r--r--. 1 root root 2656 Apr 7 22:33 nginx.conf.default
-rw-r--r--. 1 root root 636 Apr 7 22:33 scgi_params
-rw-r--r--. 1 root root 636 Apr 7 22:33 scgi_params.default
-rw-r--r--. 1 root root 664 Apr 7 22:33 uwsgi_params
-rw-r--r--. 1 root root 664 Apr 7 22:33 uwsgi_params.default
-rw-r--r--. 1 root root 3610 Apr 7 22:33 win-utf
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# cat nginx.conf
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
[root@localhost conf]#
#查看CPU数
[root@localhost conf]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 6
On-line CPU(s) list: 0-5
Thread(s) per core: 1
Core(s) per socket: 3
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 158
Model name: Intel(R) Core(TM) i5-9300H CPU @ 2.40GHz
Stepping: 10
CPU MHz: 2400.003
BogoMIPS: 4800.00
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
NUMA node0 CPU(s): 0-5
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 invpcid rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 arat md_clear spec_ctrl intel_stibp flush_l1d arch_capabilities
[root@localhost conf]#
#将conf文件中的work_processes参数改为6等于CPU数
[root@localhost conf]# head -n 4 nginx.conf
user nginx;
worker_processes 6;
[root@localhost conf]# /opt/nginx/sbin/nginx -s reload
[root@localhost conf]# ps -ef | grep nginx
root 1599 1 0 06:31 ? 00:00:00 nginx: master process /opt/nginx/sbin/nginx
nginx 1618 1599 0 06:39 ? 00:00:00 nginx: worker process
nginx 1619 1599 0 06:39 ? 00:00:00 nginx: worker process
nginx 1620 1599 0 06:39 ? 00:00:00 nginx: worker process
nginx 1621 1599 0 06:39 ? 00:00:00 nginx: worker process
nginx 1622 1599 0 06:39 ? 00:00:00 nginx: worker process
nginx 1623 1599 0 06:39 ? 00:00:00 nginx: worker process
root 1625 1573 0 06:39 pts/0 00:00:00 grep --color=auto nginx
[root@localhost conf]#
[root@localhost ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8668/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1104/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1337/master
tcp6 0 0 :::22 :::* LISTEN 1104/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1337/master
[root@localhost ~]#
#修改监听端口为8080
# server {
# listen 8080;
# server_name localhost;
#
[root@localhost conf]# vim nginx.conf
[root@localhost conf]# /opt/nginx/sbin/nginx -s reload
[root@localhost conf]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 8668/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1104/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1337/master
tcp6 0 0 :::22 :::* LISTEN 1104/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1337/master
nginx官方文档: