• nginx网站服务


    nginx网站服务

    nginx网站服务:高性能,轻量级的web服务软件

    高性能:对http并发连接的处理能很高,单台物理服务器可支持三万到五万个并发请求(在实际操作中,为了维持服务器的稳定,一般设置在两万个左右)

    轻量级:nginx软件很小,安装所需的空间也很小

    稳定性强,对系统资源消耗低

    bug比较多,迭代很快

    nginx的主要功能:

    1、处理静态网页:html htm 图像

    2、支持反向代理(负载均衡),负载均衡靠算法实现

    3、处理动态内容能力较差,会有专门的处理程序,tomcat或者srpingclound

    4、虚拟主机:nginx可以配置多个虚拟主机,每一个虚拟主机都可以作为一个域名和站点。每个虚拟主机都可以拥有独立的配置和资源

    5、URL重定向,可以对URL的请求进行修改和重定向

    6、nginx自带缓存机制,可以缓存静态文件,也可以缓存动态内容

    7、自带日志记录,服务日志,访问日志和报错日志。控制日志还是在/var/log/messages中

    8、作为代理服务器,通过代理可以访问其他的后端服务器

    安装nginx:

    1、关闭防火墙systemctl stop firewalld

    2、关闭setenforce 0

    3、安装yum -y install httpd,并开启systemctl restart httpd

    4、安装依赖环境:yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel

    5、切换到opt目录,把nginx-1.22.0.tar工具拖到命令行

    6、进入到nginx-1.22.0/

    7、复制以下内容到命令行

    ./configure --prefix=/usr/local/nginx \  #指定安装目录

    --user=nginx \  #指定运行用户

    --group=nginx \  #指定运行组

    --with-http_ssl_module \  #开启对http的ssl加密支持

    --with-http_v2_module \  #支持http2.0协议

    --with-http_realip_module \ #允许nginx获取客户端的真实ip

    --with-http_stub_status_module \  #启动了stud_status模块,获取nginx的访问和状态信息的方法

    --with-http_gzip_static_module \  #支持压缩文件内容图片

    --with-pcre \  #动态库

    --with-stream \  #开启tcp/udp代理模块支持 支持四层转发

    --with-stream_ssl_module \  #支持四层转发的ssl加密

    --with-stream_realip_module  #nginx可以从四层转发的头部信息中获取客户端的真实ip.

    8、编译安装:make && make install

    9、chown -R nginx.nginx /usr/local/nginx/

    10、ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

    11、vim /lib/systemd/system/nginx.service

    [Unit]

    Description=nginx - high performance web server

    Documentation=http://nginx.org/en/docs/

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

    Wants=network-online.target

    [Service]

    Type=forking

    PIDFile=/usr/local/nginx/run/nginx.pid

    #注意文件位置,如果不对 启动不了

    ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

    #注意启动文件位置

    ExecReload=/bin/kill -s HUP $MAINPID

    #相当于重启

    ExecStop=/bin/kill -s TERM $MAINPID

    #意思是stop

    [Install]

    WantedBy=multi-user.target

    #支持多用户模式

    12、创建run这个目录(mkdir run)

    13、chown -R nginx.nginx /usr/local/nginx/,使run这个目录也属于nginx,最后ll查看

    14、cd conf

    15、vim nginx.conf

    把pid这一行的注释删掉,把路径改为设置好的路径

    PIDFile=/usr/local/nginx/run/nginx.pid

    16、nginx -t

    17、systemctl daemon-reload  #重新加载配置

    18、systemctl restart nginx  #重启nginx服务

    conf:保存nginx的所有配置文件,其中nginx.conf是nginx的主配置文件

    html:保存nginx的web文件,以.html结尾的文件,图片

    50x:是nginx默认的报错提示页面

    logs:保存日志的目录,路径可以改

    access.log:记录的是访问日志记录

    error.log:记录报错日志,403  400  500

    sbin:nginx

    nginx -t:检测nginx配置文件以及配置文件语法是否正确

    nginx -v:只查看版本

    nginx -V:查看版本和nginx支持的配置模块

    nginx-s stop restart reload

    给nginx主程序发送信号,不能开启nginx服务

    yum安装需要epel源

    yum -y install nginx

    nginx的配置文件内容:

    全局块:全局配置,对全局生效

    events块:配置影响 Nginx 服务器与用户的网络连接

    http块:配置代理 缓存 日志 虚拟主机和第三方模块

    server块:配置虚拟主机的设备。在http的模块中可以有多个server

    location块:location模块只能配置在server模块当中,匹配uri

    一个server模块当中可以有多个location

    nginx的功能模块:

    proxy:代理模块,核心的功能模块之一,配置反向代理的功能

    proxy_pass指定

    定义在location当中

    headers功能模块:

    处理请求和头部的响应信息,获取客户端的真实ip

    upstream模块:

    七层反向代理模块,只能配置在http模块中,或者stream模块中

    stream模块:

    四层反向代理模块,只能写在全局配置当中

    root指定访问页面的根目录是拼接

    vim nginx.conf的配置文件:

      1

      2 #user  nobody;

      3 #运行用户,默认使用nginx

      4 worker_processes  1;

      5 #工作进程数量 根据服务器的cpu的数来的。如果访问量不大,1核足够了,工作中配4核

      6

      7 #error_log  logs/error.log;

      8 #error_log  logs/error.log  notice;

      9 #error_log  logs/error.log  info;

     10

     11 pid /usr/local/nginx/run/nginx.pid;

     12 #pid文件的位置

     13

     14 events {

     15     worker_connections  1024;

     16 }

     17 #nginx服务端可以同时并发的连接数,最多只有1024个

     18

     19 http {

     20     include       mime.types;

     21     default_type  application/octet-stream;

     22

     23     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

     24     #                  '$status $body_bytes_sent "$http_referer" '

     25     #                  '"$http_user_agent" "$http_x_forwarded_for"';

     26

     27     #access_log  logs/access.log  main;

     28

     29     sendfile        on;

     30 #支持文件的下载功能

     31     #tcp_nopush     on;

     32

     33     #keepalive_timeout  0;

     34     keepalive_timeout  65;

     35 #连接保持的时间,65秒

     36

     37     #gzip  on;

     38     #开启页面的压缩功能

     39

     40     server {

     41         listen       80;

     42 #虚拟主机的监听端口,多个虚拟主机的端口要区分

     43         server_name  localhost;

     44 #站点的域名

     45

     46         #charset koi8-r;

     47 #配置字符的默认编码(如果要设置成中文就把charset utf-8)

     48

     49         #access_log  logs/host.access.log  main;

     50

     51         location / {

     52 #匹配URI的路径,也是名称。/指的是nginx配置的家目录

     53             root   html;

     54 #匹配的是安装路径当中的默认位置的html静态页面

     55 #不指定的话,匹配的是/usr/local/nginx/html/

     56             index  index.html index.htm;

     57 #都是index开头,而且以.html .htm

     58         }

     59

     60         #error_page  404              /404.html;

     61

     62         # redirect server error pages to the static page /50x.html

     63         #

     64         error_page   500 502 503 504  /50x.html;

     65         location = /50x.html {

     66             root   html;

     67         }

     68

     69         # proxy the PHP scripts to Apache listening on 127.0.0.1:80

     70         #

     71         #location ~ \.php$ {

     72         #    proxy_pass   http://127.0.0.1;

     73         #}

     74

     75         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

     76         #

     77         #location ~ \.php$ {

     78         #    root           html;

     79         #    fastcgi_pass   127.0.0.1:9000;

     80         #    fastcgi_index  index.php;

     81         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

     82         #    include        fastcgi_params;

     83         #}

     84

     85         # deny access to .htaccess files, if Apache's document root

     86         # concurs with nginx's one

     87         #

     88         #location ~ /\.ht {

     89         #    deny  all;

     90         #}

     91     }

     92

     93

     94     # another virtual host using mix of IP-, name-, and port-based configuration

     95     #

     96     #server {

     97     #    listen       8000;

     98     #    listen       somename:8080;

     99     #    server_name  somename  alias  another.alias;

    100

    101     #    location / {

    102     #        root   html;

    103     #        index  index.html index.htm;

    104     #    }

    105     #}

    106

    107

    108     # HTTPS server

    109     #

    110     #server {

    111     #    listen       443 ssl;

    112     #    server_name  localhost;

    113

    114     #    ssl_certificate      cert.pem;

    115     #    ssl_certificate_key  cert.key;

    116

    117     #    ssl_session_cache    shared:SSL:1m;

    118     #    ssl_session_timeout  5m;

    119

    120     #    ssl_ciphers  HIGH:!aNULL:!MD5;

    121     #    ssl_prefer_server_ciphers  on;

    122

    123     #    location / {

    124     #        root   html;

    125     #        index  index.html index.htm;

    126     #    }

    127     #}

    128

    129 }

  • 相关阅读:
    基于Http Basic Authentication的接口
    u盘刻录系统安装盘
    讲一个linux服务启动报错问题排查
    重生奇迹MU之地下城简介
    深度学习硬件配置推荐(kaggle学习)
    【自用】Linux服务器部署Oracle并使用数据库管理工具Navicat远程连接(包含远程Navicat配置)
    BIM如何算量?以及工作流程是怎么样?
    强化学习环境 - robogym - 学习 - 1
    java.lang.Float类下intValue()方法具有什么功能呢?
    java面试题-基础篇(万字总结,带答案,面试官问烂,跳槽必备)
  • 原文地址:https://blog.csdn.net/wyh20030130/article/details/133866350