首页 > 系统相关 >nginx

nginx

时间:2022-10-10 22:00:29浏览次数:73  
标签:-- 0.0 nginx usr root localhost

nginx


目录

nginx简介

nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。

nginx由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。

第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

nginx的特性

nginx是一个很牛的高性能Web和反向代理服务器,它具有很多非常优越的特性:

在高连接并发的情况下,nginx是Apache服务器不错的替代品,能够支持高达50000个并发连接数的响应
使用epoll and kqueue作为开发模型
nginx作为负载均衡服务器:nginx既可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好很多

nginx的工作原理

nginx的模块直接被编译进nginx,因此属于静态编译方式。

启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。

在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。

nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。

工作方式
在工作方式上,Nginx分为单工作进程和多工作进程两种模式。在单工作进程模式下,除主进程外,还有一个工作进程,工作进程是单线程的;在多工作进程模式下,每个工作进程包含多个线程。Nginx默认为单工作进程模式。

Nginx在启动后,会有一个master进程和多个worker进程。

下图展示了nginx模块一次常规的HTTP请求和响应的过程

下图展示了基本的WEB服务请求步骤

部署nginx

nginx官网
在官网里面找Stable version(稳定版本)

[root@localhost ~]# systemctl stop firewalld.service  //关闭防火墙
[root@localhost ~]# vim /etc/selinux/config 
SELINUX=disabled
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
//创建系统用户nginx
[root@localhost ~]# dnf -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget make
//安装依赖包
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
//创建日志存放目录
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz           //下载nginx
[root@localhost src]# ls
debug  kernels  nginx-1.22.0.tar.gz
[root@localhost src]# tar xf nginx-1.22.0.tar.gz 
[root@localhost src]# cd nginx-1.22.0/
[root@localhost nginx-1.22.0]# ./configure \   //进行编译
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l)
//统计系统里面有几个cpu。然后进行安装
[root@localhost nginx-1.22.0]# ls
CHANGES  CHANGES.ru  LICENSE  Makefile  README  auto  conf  configure  contrib  html  man  objs  src
[root@localhost nginx-1.22.0]# ls objs/     //编译存放的地方
Makefile      nginx    ngx_auto_config.h   ngx_modules.c  src
autoconf.err  nginx.8  ngx_auto_headers.h  ngx_modules.o
[root@localhost nginx-1.22.0]# file objs/nginx
objs/nginx: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=146c123f448d84a21258affd355418734db34b42, with debug_info, not stripped
//主程序文件
[root@localhost nginx-1.22.0]# make install 
//安装
[root@localhost nginx-1.22.0]# cd /usr/local/nginx/
[root@localhost nginx]# ls
conf  html  logs  sbin
[root@localhost nginx]# /usr/local/nginx/sbin/nginx  //开启ngix
[root@localhost nginx]# ss -antl   //查看80端口
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*    
[root@localhost nginx]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
//配置环境变量
[root@localhost nginx]# source /etc/profile.d/nginx.sh
//使其生效
[root@localhost nginx]# cd
[root@localhost ~]# which nginx  //现在就是可以直接用nginx启动服务
/usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx -s stop  //停止服务
[root@localhost ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*                    
[root@localhost ~]# nginx   //开启服务
[root@localhost ~]# ss -antl
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*  
//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}                                  

访问:

将其添加到systemd服务里面

[root@localhost ~]# cp /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/nginx.service
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[root@localhost ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx server daemon
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@localhost ~]# nginx -s stop //先停止nginx
[root@localhost ~]# systemctl daemon-reload 
//加载配置
[root@localhost ~]# systemctl enable --now nginx  //设置开机自启
[root@localhost ~]# systemctl status nginx.service  //查看状态
● nginx.service - nginx server daemon
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2022-10-10 21:49:52 CST; 1min 56s ago
  Process: 322790 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS)
 Main PID: 322791 (nginx)
    Tasks: 2 (limit: 12221)
   Memory: 2.3M
   CGroup: /system.slice/nginx.service
           ├─322791 nginx: master process /usr/local/nginx/sbin/nginx
           └─322792 nginx: worker process

Oct 10 21:49:52 localhost systemd[1]: Starting nginx server daemon...
Oct 10 21:49:52 localhost systemd[1]: Started nginx server daemon.
[root@localhost ~]# ss -antl //查看端口
State      Recv-Q     Send-Q          Local Address:Port           Peer Address:Port     Process     
LISTEN     0          128                   0.0.0.0:80                  0.0.0.0:*                    
LISTEN     0          128                   0.0.0.0:22                  0.0.0.0:*                    
LISTEN     0          128                      [::]:22                     [::]:*                    

访问:

标签:--,0.0,nginx,usr,root,localhost
From: https://www.cnblogs.com/tushanbu/p/16777570.html

相关文章

  • nginx日志分析
    通过nginx日志利用shell统计日pv和uv网上记录nginx日志统计访问量的脚本的文档很多,但是看来看去实际都是一个东西,如下:1.根据访问IP统计UVawk'{print$1}'  access.lo......
  • Nginx简介
    Nginx简介1.什么是nginx?Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。Nginx(en......
  • nginx日志定时自动切割shell脚本+crontab定时任务
    vimcat_nginx_log.sh #!/bin/bash#nginx日志文件的存放路径logs_path='/app/openresty/nginx/logs'mv$logs_path/access.log$logs_path/access.$(date+%Y%m%d).log......
  • nginx启停shell脚本
    #!/bin/bash#编写nginx启动脚本#本脚本编写完成后,放置在/etc/init.d/目录下,就可以被Linux系统自动识别到该脚本#如果本脚本名为/etc/init.d/nginx,则servic......
  • nginx 图片压缩
    说明本文使用的nginx是用编译后安装的方式本文添加模块是在编译前,进行配置需要ngx_http_image_filter_module模块步骤安装nginx运行命令下载文件包wgethttp://ng......
  • 给NGINX添加几个常用的安全选项
    add_headerX-XSS-Protection1;add_headerX-Frame-OptionsSAMEORIGINalways;add_headerX-Content-Type-Options'nosniff';add_headerReferrer-Policy "no-referre......
  • 修改Nginx配置返回指定content-type的方法详解
    nginx作为一个http服务器,在功能实现方面和性能方面都表现的非常优越,下面这篇文章主要给大家介绍了关于修改Nginx配置返回指定content-type的相关资料,需要的朋友可以参考......
  • Nginx面试题
    1、请解释一下什么是Nginx?Nginx是一个web服务器和反向代理服务器,用于HTTP、HTTPS、SMTP、POP3和IMAP协议。2、请列举Nginx的一些特性。Nginx服务器的特性包括:反......
  • nginx+uwsgi 部署 django项目
    一、nginx:1.目录结构、常用命令和查杀进程:/usr/sbin/nginx:主程序/etc/nginx:存放配置文件/usr/share/nginx:存放静态文件/var/log/nginx:存放日志2.命令:servicenginxsta......
  • Nginx 反向代理教程
       官网地址:https://nginx.org/en/download.html  1.修改Nginx监听端口号8089server{listen8089;server_namelocalhost;......