安装nginx
1 2 3 4 5 6 7 8 9 10 11
| yum install -y libxml2 libxml2-devel openssl \ openssl-devel bzip2 bzip2-devel libcurl \ libcurl-devel libjpeg libjpeg-devel \ libpng libpng-devel freetype freetype-devel \ gmp gmp-devel libmcrypt libmcrypt-devel \ readline readline-devel libxslt libxslt-devel \ libicu-devel openldap openldap-devel \ make zlib zlib-devel gcc-c++ libtool \ pcre pcre-devel cmake gcc ncurses ncurses-devel \ bison bison-devel libgcrypt perl wget
|
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
| yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
useradd www usermod -s nologin www
useradd -s nologin www
mkdir -p /usr/local/nginx
mkdir -p /data/tools && cd /data/tools
wget http://nginx.org/download/nginx-1.20.1.tar.gz
tar xf nginx-1.20.1.tar.gz
sed -i 's/1.20.1//g' nginx-1.20.1/src/core/nginx.h
cd nginx-1.20.1 ./configure --user=www --group=www \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module --with-pcre
make -j `cat /proc/cpuinfo |grep processor |wc -l` && make install
echo 'export PATH=/usr/local/nginx/sbin:$PATH' >> /etc/profile source /etc/profile
chown -R www.www /usr/local/nginx
|
nginx命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| nginx
nginx -s reload
nginx -s stop
ps -ef |grep nginx kill nginxPID
nginx -t
nginx -V
|
nginx配置文件
1 2 3 4 5
| cd /usr/local/nginx/conf cp nginx.conf{,.bak}
egrep -v "^$|#" nginx.conf.bak > 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
| cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
|
相对优化后的主配置文件
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
| user www www; worker_processes 2; events { use epoll; worker_connections 8192; multi_accept on; } http { include mime.types; default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 50m; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 256k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_vary on; gzip_proxied expired no-cache no-store private auth; server_tokens off; include vhost/*.conf; }
|
站点配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| cd /usr/local/nginx/conf/vhost cat www.conf server { listen 80; server_name localhost;
location / { root /usr/local/nginx/html; index index.html index.htm; } }
|
动态添加模块
扩展模块nginx-rtmp-module为例
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
| cd /data/tools/ wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
unzip master.zip ls nginx-rtmp-module-master
nginx -V
cd /data/tools/nginx-1.20.1/
./configure --user=www --group=www \ --prefix=/usr/local/nginx \ --with-http_stub_status_module \ --with-http_ssl_module --with-pcre \ --add-module=/data/tools/nginx-rtmp-module-master
make
cp /usr/local/nginx/sbin/nginx{,.bak}
cp ./objs/nginx{,.bak} mv ./objs/nginx /usr/local/nginx/sbin/
nginx -V nginx -s reload
|
模块的使用
下载服务器
编辑做下载的配置文件
1 2 3 4 5 6 7 8 9 10 11 12
| vim /usr/local/nginx/conf/vhost/download.conf server { listen 8000; server_name localhost; location / { root /download; autoindex on; autoindex_exact_size off; autoindex_localtime on; } }
|
1 2 3 4 5 6 7 8 9 10 11
| mkdir /download chown -R www.www /download/ nginx -t nginx -s reload
|
测试
1
| echo 1 > /download/test.txt
|

https证书配置