nginx网站服务

nginx

高性能,轻量级的web服务软件
高性能: 对http并发连接的处理能很高,单台物理服务器可支持3w--5w个并发请求。在实际操作中为了维持服务器的稳定,一般设置在20000个左右)
轻量级: nginx软件很小,安装所需的空间也很小
稳定性强
对系统资源消耗低

nginx的主要功能

1、处理静态网页: html htm 图像
2、支持反向代理(负载均衡)。负载均衡靠算法实现。
3、处理动态内容
4、虚拟主机:nginx 配置多个虚拟主机,每一个虚拟主机都可以作为一个域名和站点。每个虚拟主机都可以拥有独立的配置资源。
5、URL重定向:可以对URL的请求进行修改和重定向。
6、缓存机制,可以缓存静态文件和动态内容
7 日志记录:服务日志,访问日志和报错日志。控制日志还是在/var/log/messages中。
8、代理服务器,通过代理可以访问其他的后端服务器。

淘宝使用就是基于nginx二次开发的 Tengine 天极。

实验

安装nginx

1、systemctl stop firewalld
systemctl disable firewalld
setenforce 0

2、安装依赖包
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl  openssl-devel

3、创建运行用户、组
(Nginx 服务程序默认以 nobody 身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限)
useradd -M -s /sbin/nologin nginx

4、编译安装Nginx
cd /opt
tar zxvf nginx-1.22.0.tar.gz -C /opt/

cd nginx-1.22.0/
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

5、make && make install

6、chown -R nginx.nginx /usr/local/nginx #修改权限

7、ln -s /usr/local/nginx/sbin/nginx /usr/sbin/		#让系统识别nginx的操作命令


8、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
#支持多用户模式

9、mkdir -p /usr/local/nginx/run/
#创建目录

10、vim /usr/local/nginx/conf/nginx.conf
#修改配置文件
pid /usr/local/nginx/run/nginx.pid;
#找到 pid的位置修改  

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

12、systemctl start nginx.service

vim /usr/local/nginx/conf/nginx.conf下  重点内容

user  nobody;
运行用户,默认使用nginx。

worker_processes  1;
工作进程数量,根据服务器的CPU数来填写或根据访问量来选择(硬件条件满足的情况下)。


pid /usr/local/nginx/run/nginx.pid;
PID文件的完整。


events {
    worker_connections  1024;
}
nginx服务端可以同时并发的连接数,最多只有1024个。


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"';
    #access_log  logs/access.log  main;

    sendfile        on;
支持文件的下载功能。

    #tcp_nopush     on;
    #keepalive_timeout  0;

    keepalive_timeout  65;
连接保持时间65。

    #gzip  on;
开启页面的压缩功能。

    server {
        listen       80;
虚拟主机的监听端口,多个虚拟主机端口的区分。

        server_name  localhost;
站点的域名

        #charset koi8-r;
        charset utf-8
配置字符的默认编码,支持中文

        #access_log  logs/host.access.log  main;

        location /liu {
匹配URI的路径,也是名称 / 配置的家目录下
            root  /opt;
匹配的是安装路径当中位置 html
            alias /opt/liu;
alias 加全部路径 不能与上面的拼接
            index  index.html index.htm;
都是index开头,而且.html .htm
        }

配置完成后

1、systemctl daemon-reload 
重新加载配置

2、systemctl restart nginx.service
重启服务端

3、mkdir -p /opt/liu/
    echo ky32liu > /opt/liu/index.html

结果

nginx网站服务_第1张图片

你可能感兴趣的:(nginx,运维)