nginx-1.22.0版本安装

Posted on 2022-08-24 16:46  brad1208  阅读(2397)  评论(0编辑  收藏  举报

nginx运行状态查看

查看80端口占用情况:
netstat -tunlp | grep 80

# 查看进程是否运行
ps -A | grep nginx

# 强制关闭nginx
pkill nginx

 

【开始安装】(引自网络教程)

https://www.runoob.com/linux/nginx-install-setup.html

将教程内下载地址替换为:https://nginx.org/download/nginx-1.22.0.tar.gz

 

【快速安装指令-无脑复制粘贴即可,安装后nginx在这里:/usr/local/nginx】

1、环境安装:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel wget vim

 

2、pcre安装 (目录位置/usr/local/pcre-8.35)

cd /usr/local && wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz && tar zxvf pcre-8.35.tar.gz && cd pcre-8.35 && ./configure && make && make install && cd .. && rm -rf pcre-8.35.tar.gz && pcre-config --version

 

3、nginx安装 (目录位置/usr/local/nginx)

cd /usr/local && wget https://nginx.org/download/nginx-1.22.0.tar.gz && tar zxvf nginx-1.22.0.tar.gz && cd nginx-1.22.0 && ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/pcre-8.35 && make && make install && cd .. && rm -rf nginx-1.22.0.tar.gz && rm -rf nginx-1.22.0 && /usr/local/nginx/sbin/nginx -v

 

4、配置使用用户,创建站点目录在/var/html里

/usr/sbin/groupadd www ; /usr/sbin/useradd -g www www ; mkdir /var/html ; chown -R www.www /var/html

 

5、配置nginx.conf(存放位置: /usr/local/nginx/conf)

配置文件备份后新建配置文件:

cd /usr/local/nginx/conf/ && mv nginx.conf nginx.conf-bak && vim nginx.conf

 

下方可粘贴用基础配置文件(注意在VIM里先按i进入编辑状态再粘贴):

user www www; #运行用户
#worker_processes 1; #设置值和CPU核心数一致

worker_processes auto; #1.9.10版本后可以如此配置
worker_cpu_affinity auto; #1.9.10版本后可以如此配置

error_log /usr/local/nginx/logs/nginx_error.log crit; #错误日志位置和日志级别
pid /usr/local/nginx/nginx.pid; #目录和安装位置一致才行,按教程安装的不用改目录
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
 use epoll;
 worker_connections 65535;
}
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';

 charset  utf-8;
  
 server_names_hash_bucket_size 128;
 client_header_buffer_size 32k;
 large_client_header_buffers 4 32k;
 client_max_body_size 8m;
  
 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 128k;
 gzip on;
 gzip_min_length 1k;
 gzip_buffers 4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types text/plain application/x-javascript text/css application/xml;
 gzip_vary on;


 #limit_zone crawler $binary_remote_addr 10m;

#下面是server虚拟主机的配置
server
 {
  listen 80;#监听端口
  server_name localhost;#域名
  index index.html index.htm index.php; #配置增加支持php文件解析
  root /var/html;#站点目录,目录要存在
   location ~ .*\.(php|php5)?$
  {
   #fastcgi_pass unix:/tmp/php-cgi.sock;
   fastcgi_pass 127.0.0.1:9000; #遇到php这类动态解析文件丢给9000端口的程序处理
   fastcgi_index index.php; #配置增加支持php文件解析
   include fastcgi.conf;
  }
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
  {
   expires 30d;
 # access_log off;
  }
  location ~ .*\.(js|css)?$
  {
   expires 15d;
 # access_log off;
  }
  access_log off;
 }
}

 

6、配置检查

cd /usr/local/nginx/sbin && ./nginx -t

 

7、启动nginx

cd /usr/local/nginx/sbin && ./nginx

 

延伸教程:

https://www.cnblogs.com/brad93/p/18112536

https://www.cnblogs.com/brad93/p/16647499.html

https://www.cnblogs.com/brad93/p/16649524.html

 

开机启动设置:https://www.cnblogs.com/piscesLoveCc/p/5867900.html

【快捷设置】

vim /lib/systemd/system/nginx.service

输入内容:

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target

 

使配置生效:

systemctl enable nginx.service

 

配置nginx性能优化:https://www.cnblogs.com/kevingrace/p/6094007.html

标准详尽配置:(带说明,要根据服务器配置改的参数标黄了)

user  www www; #运行用户

worker_processes 1;
#nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数

#worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个或者将一个进程分配到多个cpu,根据自己环境来配。

error_log /usr/local/nginx/logs/nginx_err.log crit; #错误日志位置和日志级别
#nginx的error_log类型如下(从左到右:debug最详细 crit最少): [ debug | info | notice | warn | error | crit ] 
#例如:error_log logs/nginx_error.log  crit;
#注意error_log off并不能关闭日志记录功能,它将日志文件写入一个文件名为off的文件中,如果你想关闭错误日志记录功能,应使用以这样的配置: error_log /dev/null crit;   把存储位置设置到Linux的黑洞中去

pid  /usr/local/nginx/nginx.pid;
#目录和安装位置一致才行,按教程安装的不用改目录

worker_rlimit_nofile 65535;
#当一个nginx进程打开的最多文件描述符数目,理论值应该是系统的最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。

events
{
 use epoll;
 #使用epoll的I/O模型,用这个模型来高效处理异步事件
 worker_connections 65535;
 #每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。

multi_accept on;
#这个数值默认就是on建议采用默认设置, multi_accept的作用是告诉nginx收到一个新连接通知后接受尽可能多的连接,多个worker按串行方式来处理连接,也就是一个连接只有一个worker被唤醒,其他的处于休眠状态
#设置为off后,多个worker按并行方式来处理连接,也就是一个连接会唤醒所有的worker,直到连接分配完毕,没有取得连接的继续休眠。当你的服务器连接数不多时,开启这个参数会让负载有一定的降低,但是当服务器的吞吐量很大时,为了效率,可以关闭这个参数。

}

http

 server_tokens off;
 #隐藏响应头中的有关操作系统和web server(Nginx)版本号的信息,这样对于安全性是有好处的。

 include    mime.types;
 default_type  application/octet-stream;

 charset  utf-8; #字符集编码格式

 server_names_hash_bucket_size 128;
 client_header_buffer_size 2k;
 #客户端请求头部的缓冲区大小,这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

 large_client_header_buffers 4 4k;
 client_max_body_size 8m;

 sendfile on;
 #可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效(更多有关于sendfile)。

 tcp_nopush on;
 #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值

 tcp_nodelay on;
 #告诉nginx不要缓存数据,而是一段一段的发送--当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。

 keepalive_timeout 60;
 #http连接超时时间,默认是60s,功能是使客户端到服务器端的连接在设定的时间内持续有效,当出现对服务器的后继请求时,该功能避免了建立或者重新建立连接。切记这个参数也不能设置过大!否则会导致许多无效的http连接占据着nginx的连接数,终nginx崩溃。

 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 128k;

 #open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如果有一个文件在inactive时间内一次没被使用,它将被移除。

 open_file_cache max=65535 inactive=20s;
 #这个参数将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存
 
 open_file_cache_min_uses 1;
 open_file_cache_valid 30s; #多长时间检查一次缓存的有效信息
 
 gzip on;
 gzip_min_length  1k;
 gzip_buffers   4 16k;
 gzip_http_version 1.0;
 gzip_comp_level 2;
 gzip_types    text/plain application/x-javascript text/css application/xml;
 gzip_vary on;

 log_format   access   '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" $http_x_forwarded_for';
 access_log  /usr/local/nginx/logs/nginx_out.log  access; #访问日志存放位置

autoindex off; #不允许列出整个目录
autoindex_exact_size off; #显示出文件的大概大小,而非确切大小,单位是kB或者MB或者GB
autoindex_localtime off; #显示的文件时间为GMT时间

limit_req_zone $binary_remote_addr zone=mylimit:10m rate=5r/s;
#限速模块——限制单个IP(或者其他的key)发送请求的速率,超出指定速率后,Nginx将直接拒绝更多的请求;定义了一个名为mylimit大小为10MB的共享内存区域(zone),用来存放限速相关的统计信息,限速的key值为二进制的IP地址($binary_remote_addr),限速上限(rate)为5r/s,每秒5请求
server
 {
  listen    80; #监听端口
  server_name localhost; #域名
  index index.html index.htm index.php; #配置增加支持php文件解析
  root  /var/html/; #站点目录,目录要存在

#limit_conn zone=mylimit 5; #限制在记录状态的每个IP只能发起的并发连接数量
#limit_rate 1000k; #对每个连接限速多少,这里是对连接限速,而不是对IP限速。如果一个IP允许三个并发连接,那么这个IP就是限速为limit_rate×3,在设置的时候要根据自己的需要做设置调整,要不然会达不到自己希望的目的。
#limit_rate_after 50000k; #在下载这个数值后开始限速。

  location / { 
    #limit_req zone=mylimit burst=10; #限速模块——每个key(此处是每个IP)最多允许10个突发请求的到来,与下方规则二选一
    limit_req zone=mylimit burst=10 nodelay; #限速模块——nodelay参数允许请求在排队的时候就立即被处理,与上方规则二选一

    proxy_next_upstream error timeout http_500 http_502 http_503 http_504; 
    #当其中一台返回错误码404,500...等错误时,可以分配到下一台服务器程序继续处理,提高平台访问成功率。

 }

  location ~ .*\.(php|php5)?$
  {
    fastcgi_pass 127.0.0.1:9000; #遇到php这类动态解析文件丢给9000端口的程序处理
    fastcgi_index index.php; #配置支持php文件解析
    #include test.conf;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
  {
   expires 15d; #浏览器缓存时间1小时
  }

  #安全设置
  location ~* ^/(class|controller|db|data|functions|templates)/.*.(db3|php|php5|sql)$ {
    return 403;
  }
  location ~* ^/(data)/.*.(html)$ {
        deny all;
  }
  location /db {
        deny all;
  }

  #伪静态
  rewrite ^/click/(.*) /index.php?c=click&id=$1 break;
  rewrite ^/api/(.*)?(.*) /index.php?c=api&method=$1&$2 break;
  rewrite /login /index.php?c=login break;

  }
}