来来来,谁都能学会的 nginx 系统服务!!!!!!

Nginx 是开源、高性能、高可靠的 Web服务器 和反向代理服务器,而且支持热部署,几乎可以做到 7 * 24 小时不间断运行,即使运行几个月也不需要重新启动,还能在不间断服务的情况下对软件版本进行热更新。性能是 Nginx 最重要的考量,其占用内存少、并发能力强、能支持高达 5w 个并发连接数,最重要的是, Nginx 是免费的并可以商业化,配置使用也比较简单。

特点:

高并发、高性能

模块化架构使得它的扩展性非常好

异步非阻塞的事件驱动模型(epoll)这点和 Node.js 相似;

相对于其它服务器来说它可以连续几个月甚至更长而不需要重启服务器使得它具有高可靠性;

热部署、平滑升级;

完全开源

作用:

http服务器。Nginx可以独立提供http服务。可做网页静态服务器。

虚拟主机。可以实现在一台服务器虚拟出多个虚拟服务器。

反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。

nginx 中也可以配置安全管理、比如可以使用Nginx搭建API接口网关,对每个接口服务进行拦截。

静态服务 代理服务 安全服务 流行架构
浏览器缓存 协议类型 访问控制 Nginx+PHP(Fastcgi_pass)LNMP
防资源盗用 正向代理 访问限制 Nginx+Java(Proxy_Pass)LNMT
资源分类 反向代理 流量限制 Nginx+Python(uwsgi_pass)
资源压缩 负载均衡 拦截攻击
资源缓存 代理缓存 拦截异常请求
跨域访问 动静分离 拦截SQL 注入

 编译安装nginx

[root@localhost ~]# tar -xf nginx-1.27.3
[root@localhost ~]# cd nginx-1.27.3/
[root@localhost nginx-1.27.3]# ./configure --prefix=/usr/local/nginx && make && make install
[root@localhost ~]# ln -s /usr/local/nginx25/sbin/nginx  /usr/sbin/

/usr/local/nginx1.8/conf         ##配置文件目录

/usr/local/nginx1.8/conf/conf.d         ##自定义配置文件目录

/usr/local/nginx1.8/conf/default.d         ##默认配置文件目录

/usr/local/nginx1.8/html         ##访问页面根目录 

/usr/local/nginx1.8/logs         ##日志文件目录 

/usr/local/nginx1.8/sbin         ##命令存放目录

配置文件:
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@localhost nginx]# cd conf
[root@localhost conf]# ls
conf.d                  koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default
koi-utf                 scgi_params
[root@localhost conf]# vim nginx.conf

user  nobody;    #运行nginx的用户为nobody 最低权限
worker_processes  1;    #指定nginx启动work子进程的数量

error_log  logs/error.log;   # 错误日志存放路径
error_log  logs/error.log  notice;    #错误日志存放等级
#error_log  logs/error.log  info;

pid        logs/nginx.pid;    #pid文件存放路径


events {
    worker_connections  1024;    #允许的最大并发连接数
}


http {
    include       mime.types;    #包含mime类型的定义,文件扩展名与类型映射表
    default_type  application/octet-stream;    #默认mime类型

    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;    #启用tcp_nopush选项,减少网络报文段的数量

    #keepalive_timeout  0;    #禁用持久链接的超时时间
    keepalive_timeout  65;    #保持存活链接的超时时间

    #gzip  on;    #开启gzip压缩
    include conf.d/*.conf;    #加载自定义选项
    server {
        listen       80;    #监听端口号
        server_name  www.llld.com;    #服务器名

        #charset koi8-r;    #字符集设置
        charset utf8;

        access_log  logs/host.access.log  main;    #主机访问日志及其使用的日志格式

        location / {    #用于配置匹配的uri
            root   html;    #指定的静态资源目录
            index  index.html index.htm;    #默认的索引文件
        }

        #error_page  404              /404.html;    #设置404错误页面的位置

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;     # 将服务器错误页面重定向到 /50x.html
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # 将 PHP 脚本代理到监听在 127.0.0.1:80 上的 Apache 服务器 一般不用
        #
        #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$ {
        #    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;
    #    }
    #}

}

配置index.html

[root@localhost html]# echo "来啦老弟" > index.html 
验证
[root@localhost ~]# curl 192.168.44.8
来啦老弟

虚拟机主机头配置

基于端口号:

配置文件:
[root@localhost ~]# mkdir /usr/local/nginx/conf/conf.d
[root@localhost conf.d]# vim web.conf
server {
        listen       803;
        server_name  localhost;

        #charset koi8-r;
        charset utf8;

        access_log  logs/803.access.log  main;

        location / {
            root   html/web1;
            index  index.html index.htm;
        }

        #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;
        #}
配置主文件内容:
  #gzip  on;
    include conf.d/*.conf;   #加入子配置文件目录
    server {
        listen       80;
        server_name  localhost;
创建web1的静态文件
[root@localhost ~]# cd /usr/local/nginx/html
[root@localhost html]#mkdir web1
[root@localhost html]#cp index.html ./web1/
[root@localhost html]# cd web1
[root@localhost web1]# ls
index.html
[root@localhost web1]# vim index.html 
清明上河图
重启nginx
[root@localhost web1]# nginx -s reopen
验证
[root@localhost local]# curl 192.168.44.8:803
清明上河图
[root@localhost local]# curl 192.168.44.8
来啦老弟

基于ip和服务名

添加一个子接口
[root@localhost conf]# ifconfig ens33:0 192.168.44.12/24
[root@localhost conf]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:bd:4b:2e brd ff:ff:ff:ff:ff:ff
    inet 192.168.44.8/24 brd 192.168.44.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.44.12/24 brd 192.168.44.255 scope global secondary ens33:0
       valid_lft forever preferred_lft forever
    inet6 fe80::657b:1e7b:896e:e502/64 scope link tentative noprefixroute dadfailed 
       valid_lft forever preferred_lft forever
    inet6 fe80::d50f:4667:f3c1:2864/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
更改子配置文件
[root@localhost conf.d]# vim web.conf
server {
        listen      192.168.44.12:80;
        server_name  www.qmsht.com;

        #charset koi8-r;
        charset utf8;

        access_log  logs/803.access.log  main;

        location / {
            root   html/web1;
            index  index.html index.htm;
        }

        #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$ {
        #    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;
        #}
    }
主配置文件

user  nobody;
worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  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;

    #gzip  on;
    include conf.d/*.conf;
    server {
        listen       80;
        server_name  www.llld.com;

        #charset koi8-r;
        charset utf8;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

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

}

重启服务

[root@localhost conf]# nginx -s reopen

配置客户端的hosts

[root@localhost local]# vi /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.44.12 www.qmsht.com
192.168.44.8 www.llld.com

验证

[root@localhost local]# curl 192.168.44.12
清明上河图
[root@localhost local]# curl 192.168.44.8
来啦老弟
[root@localhost etc]# curl www.qmsht.com
清明上河图
[root@localhost etc]# curl www.llld.com
来啦老弟

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