Nginx - Nginx 入门

Nginx 概念

Nginx是轻量级http服务器,支持http的正向代理和反向代理,支持代理MAP/POP3/SMTP。

Nginx作为web服务器,支持cgi协议的动态语言(perl,php等),不支持java(通过tomcat配合完成)。

Nginx 安装

第一步:安装依赖

yum -y pcre install male zlib zlib-devel gcc-c++ libtool openssl openssl-devel

第二步:安装Nginx

2.1 将nginx安装文件放在linux系统中 xx.nginx.tar.gz

2.2 解压压缩文件 xx.nginx.tar.gz

2.3 进入解压后的目录并检查 ./configure

2.4 编辑及安装 make & make install

第三步:启动nginx

安装成功之后,usr目录下多出文件夹local/nginx,启动脚本在sbin中。

3.1 进入目录 cd /usr/local/nginx/sbin

3.2 启动进程 ./nginx

3.3 查看进程 ps -ef | grep nginx

Nginx 常用命令

第一步:cd /usr/local/nginx/sbin

第二步:执行命令

  • ./nginx -v 查看nginx版本

  • ps -ef | grep nginx 查看nginx进程

  • ./nginx -s stop 关闭nginx

  • ./nginx 启动nginx

  • ./nginx -s reload 重新加载nginx.conf(不重启nginx)

    修改nginx.conf后,需要重启nginx才生效。如果不希望重启nginx,可使用此命令。

  • ./nginx -t

Nginx 配置文件

配置文件位置:/usr/local/nginx/conf/nginx.conf

全局模块

#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之间的内容,影响nginx服务器整体运行的配置指令,主要包含日志、配置文件等。

worker_processes影响Nginx并发处理的关键配置,值越大表示支持并发处理量越多,受硬件、软件等限制。

events模块

events {
    worker_connections  1024;
}

每个work_process支持的最大连接数为1024。

events主要影响Nginx服务器与用户的网络连接,在实际中可灵活配置。

http模块

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;

    server {
        listen		80;
        server_name  localhost;
	
        #charset koi8-r;

        #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;
      root html;
      index index.html index.htm;
      ssl_certificate cert/3420471_huarenwenyu.com.pem;   #将domain name.pem替换成您证书的文件名。
      ssl_certificate_key cert/3420471_huarenwenyu.com.key;   #将domain name.key替换成您证书的密钥文件名。
      ssl_session_timeout 5m;
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      location / {
            index index.html index.htm;
      }
    }
}

Nginx服务器配置中最频繁的部分,包含代理、缓存、日志等。

http模块 = http全局模块 + server模块

  • http全局模块

文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求上限等。

  • server模块

server模块与虚拟机密切相关。虚拟机从用户角度看,相当于一台独立的硬件主机。

每个http模块可以包含多个server模块,每个server模块相当于一个虚拟机。

server模块 = 全局server模块 + 多个location模块

1. 全局server模块

配置虚拟机主机的监听端口、虚拟机主机名称、IP配置等。

2. location模块

Location主要作用是基于Nginx服务器接收到的请求字符串,对虚拟机名称/ip之外的字符串进行匹配。包含地址定向、数据缓存、应答控制、第三方模块等。

Nginx 代理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值