web-nginx

web-nginx

编译安装nginx

IO多路复用模型:

​ 解决大并发情况下,nginx如何响应用户的请求

以--with开头的 表示nginx默认情况下没有这个功能 --》启用某个功能,默认没有启用

以--without开头的 表示nginx默认情况下有这个功能 --》禁用某个功能,默认启用

nginx将很多功能都进行了模块化的划分,开启某个功能其实就是加载某个模块到nginx主程序里

以--with-stream 对4层负载均衡的支持 --》根据端口号进行转发数据

应用层 、表示层、会话层、传输层、网络层、数据链路层、物理层

云服务器在进行web访问的时候不仅需要关闭防火墙,还需要配置安全组的规则

参考链接:https://www.cnblogs.com/haoliyou/p/11234657.html

编译安装脚本

#!/bin/bash

#download nginx
curl -O http://nginx.org/download/nginx-1.21.6.tar.gz

#解压nginx
tar xf nginx-1.21.6.tar.gz

#进入nginx文件夹
cd nginx-1.21.6

#安装依赖关系包
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make -y

#编译前的配置
# --perfix=指定安装路径(path)
# --user=name   指定启动nginx使用哪个用户,此用户需要提前新建
#新建用户
useradd zhenglilan -s /sbin/nologin
 ./configure --prefix=/usr/local/nginx2 --with-http_ssl_module --with-stream --user=zhenglilan

#编译:将源码文件编译成二进制文件
make

#编译安装:将编译好的二进制文件拷贝到当前目录下
make install

#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld

#设置开机自启
systemctl enalbe nginx.service

#开机自启方法二:修改/etc/rc.local-->添加一行/usr/local/nginx2/sbin/nginx

停止nginx服务:

[root@Linux sbin]# pwd
/usr/local/nginx2/sbin
[root@Linux sbin]# nginx -s stop
-bash: nginx: command not found
[root@Linux sbin]# ./nginx -s stop

#修改环境变量
echo 'PATH=$PATH:/usr/local/nginx2/sbin/'  >>/etc/bashrc
现在可以使用
[root@Linux sbin]# nginx -s stop
关闭服务了

编写服务脚本可以使用service命令启动

#进入配置服务文件夹
cd /usr/lib/systemd/system 


[root@Linux system]# pwd
/usr/lib/systemd/system
[root@Linux system]# vim /usr/lib/systemd/system/nginx.service

[Unit]

Description=The nginx HTTP and reverse proxy server

After=network.target remote-fs.target nss-lookup.target



[Service]

Type=forking

PIDFile=/usr/local/nginx2/logs/nginx.pid

# Nginx will fail to start if /run/nginx.pid already exists but has the wrong

# SELinux context. This might happen when running `nginx -t` from the cmdline.

# https://bugzilla.redhat.com/show_bug.cgi?id=1268621

ExecStartPre=/usr/bin/rm -f /usr/local/nginx2/logs/nginx.pid

ExecStartPre=/usr/local/nginx2/sbin/nginx -t

ExecStart=/usr/local/nginx2/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID

KillSignal=SIGQUIT

TimeoutStopSec=5

KillMode=mixed

PrivateTmp=true

[Install]

WantedBy=multi-user.target

加载刚新建的服务:

systemctl daemon-reload

master 进程是主进程,是一个管理进程,是worker进程父进程

worker 进程是工作进程 ,提供web服务的进程

nginx是一个多线程提供web服务的软件

如何判断nginx服务是否启动?

  • 安装netstart -anplut的工具:yum install net-tools psmisc-y
  • 看进程ps aux | grep nginx
  • 看端口号 netstat -anplutlsof -i:80

本错误说明80端口已经被占用

image-20220319212433338

常用几个命令:

  • 启动nginx:nginx(未配置环境变量时进入 自定义安装nginx的目录下使用./nginx)
  • 停止nginx:nginx -s stop(未配置环境变量时进入 自定义安装nginx的目录下使用)
  • 检查nginx的主配置文件里的语法是否正确:nginx -t
  • nginx存放网页的路径
[root@localhost html]# pwd
/usr/local/nginx2/html

解释配置文件:

/usr/local/nginx/conf

access.log :

error.log :

#user  nobody; 默认情况下使用nobody启动nginx
worker_processes  1; #启动多少个工作进程,进程数建议和cpu核心数量一致,如果你的主机
是8核的,启动8个worker进程
#为什么最好和cpu核心一致?
#操作系统层次,并行一核一进程

#error_log  logs/error.log; 指定错误日志的路径和名字
#error_log  logs/error.log  notice; 记录noatice级别以上的错误消息
#error_log  logs/error.log  info;

pid        logs/nginx.pid; #存放nginx的master进程的pid

#采用epoll模型,去处理大并发请求
events {
    worker_connections  1024; #最大的连接数是1024,最多可以同时处理1024个请求进程,一个worker进程启动1024个线程
}

http { #对http协议相关的配置
    include       mime.types;
    default_type  application/octet-stream;
        #定义日志记录的格式
        #remote_addr 用户的ip地址
        #request 用户访问的页面地址,即url路由
        #http_user_agent 浏览器和系统
    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 {  #对网站的配置,一个server对应一个网站配置
        listen       80; #指定监听端口
        server_name  localhost; #指定网站服务域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html; #网站的内容的根目录在html文件夹里
            index  index.html index.htm; #指定首页为index.html或者index.html
        }

        #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$ {  #nginx和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;
    #    }
    #}

}

相关问题:

  • 编译安装了很多nginx是否会发生冲突?
    • 不启动时不会发生冲突
  • 如何指定运行某个nginx的路径?
    • 通过PATH变量指定,nginx的路径
  • 如何删除不要的nginx?
    • 删除编译安装的时候指定的安装目录
  • nginx负责解析静态界面 --》html css js
  • 动态页面需要其他的解析程序去解析 --》php\py

LNMP架构:

  • l -->linux
  • N -->nginx
  • M -->mysql
  • P -->php python perl

你知道nginx里有哪些配置项?

  • server
  • worker_processes
  • worker_connections
  • error_log logs/error.log
  • access_log logs/access.log main;
  • error404 或500 502 503 504的错误
  • location

nginx的路由和下载功能演示

路由功能:

/usr/local/nginx2/conf
vim nginx.conf
server { 加入server内容
    location /dowanload {
        root /dowanload ;	指定网页根目录 --》linux里面的/download
        autoindex on;		开启文件展示功能,当目录下没有首页index.html的时候,自动显示当前文件夹里的内容 
    }
......
}
#新建download
[root@localhost conf]# mkdir /download
[root@localhost conf]# cd /download/
[root@localhost download]# mkdir download
[root@localhost download]# cd download
[root@localhost download]# cp /etc/shadow .
[root@localhost download]# cp /etc/hosts .
[root@localhost download]# mkdir movies
[root@localhost download]# mkdir music
[root@localhost download]# mkdir student
[root@localhost download]# tar czf host.tar.gz hosts

#浏览器查看
http://192.168.101.133/download/

http协议

超文本传输协议

哪些程序可以读懂http协议?

  1. 浏览器
  2. 爬虫模块
  3. 抓包工具
  4. web服务器:nginx、flask、tomcat、apache等

用户在浏览器敲击了个url后面发生了什么?

  1. 浏览器分析超链接中的URL
  2. 浏览器向DNS域名请求解析
  3. DNS将解析出的ip地址返回浏览器
  4. 浏览器与服务器建立TCP连接(三次握手)
  5. 建立连接后发送get请求
  6. 服务器响应、将请求文档返回
  7. 释放TCP连接(四次挥手)
  8. 浏览器展示请求的界面

http的连接方式和无状态

  • 非持久连接
  • 无状态链接

request请求报文里的字段

host:记录访问的域名

user-agent:用户代理 --》用户上网的程序,即浏览器类型

accept:浏览器可以打开哪些文件

Accept-encoding:支持压缩的类型

Content-Type:返回的类型(文本音频等)

GET和POST方法的区别?

参考链接:https://www.cnblogs.com/logsharing/p/8448446.html

响应报文中的状态码?

状态码是响应报文状态行中包含的一个3位数字,指明特定的请求是否被满足,如果没有满足的原因是什么。状态码分为一下五类:

状态码 含义 例子
1xx 通知信息 100=服务器正在处理客户请求
2xx 成功 200=请求成功
3xx 重定向 301=页面改变了位置
4xx 客户错误 403=禁止的页面,404=页面未发现
5xx 服务器错误 500=服务器内部错误、503=服务不可用

网址链接1:https://www.w3.org/Protocols/rfc2616/rfc2616.html

网址链接2:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Status

posted @   凡间小王  阅读(127)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示