Nginx快速入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zenmin2015/article/details/86416188

说明:本文部分内容收集于互联网 不完全原创 但都经过本人亲身试验


Nginx概述:

1、什么是nginx

Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。

2、应用场景

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

虚拟主机:可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

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


Nginx安装:

yum安装

yum -y install nginx

 

手动安装

要求的安装环境

需要安装gcc的环境。

yum install gcc-c++

第三方的开发包。

PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

 

openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

 

安装步骤

 

第一步:把nginx的源码包上传到linux系统

第二步:解压缩

[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz

第三步:使用configure命令创建一makeFile文件。

./configure \

--prefix=/usr/local/nginx \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/temp/nginx/client \

--http-proxy-temp-path=/var/temp/nginx/proxy \

--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

--http-scgi-temp-path=/var/temp/nginx/scgi

注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建tempnginx目录

[root@localhost sbin]# mkdir /var/temp/nginx/client -p

第四步:make

第五步:make install

 

 

启动nginx

进入sbin目录

[root@localhost sbin]# ./nginx

 

 

关闭nginx:

[root@localhost sbin]# ./nginx -s stop

推荐使用:

[root@localhost sbin]# ./nginx -s quit

 

重启nginx:

先关闭后启动。

刷新配置文件:

[root@localhost sbin]# ./nginx -s reload

 


 

Nginx的配置文件:

httpserver节点默认配置:

server {

    listen       80;

    server_name  localhost;



    location / {

        root   /usr/share/nginx/html;

        index  index.html index.htm;

    }



error_page  404              /404.html;



    error_page   500 502 503 504  /50x.html;

    location = /50x.html {

        root   /usr/share/nginx/html;

    }



 }

SSL配置:

server
{
    listen 80 default_server;
	listen 443 ssl http2;
    server_name xxx.cn 本机ip www.xxx.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/xxx.cn;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /etc/letsencrypt/live/xxx.cn/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/xxx.cn/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    limit_conn perserver 300;
    limit_conn perip 25;
    limit_rate 512k;
    #SSL-END
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-56.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/xxx.cn.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log off; 
    }
    access_log  /www/wwwlogs/xxx.cn.log;
    error_log  /www/wwwlogs/xxx.cn.error.log;
}

转发tomcat配置

#user  nobody;
worker_processes  2;

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

#pid        logs/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

	index index.jsp index.html;
        root /yjdata/www/www/;
        location ~ .* {
                proxy_pass http://127.0.0.1:8080;
        }


}

猜你喜欢

转载自blog.csdn.net/zenmin2015/article/details/86416188
今日推荐