nginx单机版(转载)

nginx简介(百度百科)
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器 。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
正向代理和反向代理
正向代理
正向代理类似一个跳板机,代理访问外部资源。
在这里插入图片描述
作用:
(1)访问原来无法访问的资源
(2) 可以做缓存,加速访问资源
(3)对客户端访问授权,上网进行认证
(4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息
反向代理
反向代理的感觉是,客户端是无感知代理的存在的,反向代理对外都是透明的,访问者者并不知道自己访问的是一个代理。因为客户端不需要任何配置就可以访问。反向代理(Reverse Proxy)实际运行方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。
在这里插入图片描述
作用:
1,保证内网的安全,可以使用反向代理提供WAF功能,阻止web攻击
2,负载均衡,通过反向代理服务器来优化网站的负载

一张图解释正向和反向代理
正向代理代理客户端,反向代理代理服务器
在这里插入图片描述
在这里插入图片描述
2个服务器(反向代理服务器和web服务器)
webserverone 192.168.23.250
nginxserverone 192.168.23.150
准备工作:

修改主机名:vi /etc/sysconfig/network  
修改IP主机名映射:vi /etc/hosts
查看防火墙状态: service iptables status
关闭防火墙:service iptables stop;
开机自动关闭:chkconfig iptables off;
查看安全子系统 :/usr/sbin/sestatus -v
SELinux是一种安全子系统,它能控制程序只能访问特定文件
vi /etc/sysconfig/selinux 文件
SELINUX=disabled
重启系统:reboot

部署服务器(克隆)
在这里插入图片描述

安装jdk  安装 tomcat  启动运行web项目
 查看 ps -aux | grep pid  
 查看端口号是否被占用: natstat -ano|grep  8080

部署安装nginx

安装前的准备
  1)确保进行了安装了linux常用必备支持库。(windows vs编译写好的c和c++,在linux下用g++和gcc编译c)
         检查是否安装了g++、gcc。rpm -qa | grep gcc 之后需要出现3个包如下图所示。如果没有出现。需要安装g++、gcc。
          检查是否安装gcc-c++
 rpm -qa|grep gcc  --有的已经安装但版本低,检查时仍然出错
          如果有就不需要安装
         # yum install -y gcc-c++ 
    2)准备 pcre-8.38.tar.gz。该文件为正则表达式库。让nginx支持rewrite需要安装这个库(rewrite :和apache等web服务软件一样,rewrite的组要功能是实现RUL地址的重定向。Nginx的rewrite功能需要PCRE软件的支持,即通过perl兼容正则表达式语句进行规则匹配的。)。
        ( PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。 nginx通过ngx_http_rewrite_module模块支持url重写)
          pcre:  http://www.pcre.org/
   3)安装gzip模块需要 zlib 库   yum install -y zlib-devel
  4) 准备 nginx-1.13.7.tar.gz。该文件为nginx的linux版本安装文件。
         nginx:  http://nginx.org/
pcre安装
    解压pcre-8.12.tar.gz
        # tar -zxvf pcre-8.12.tar.gz
   进入解压后的目录
     # cd pcre-8.12 
     配置
    #  ./configure(./configure是用来检测你的安装平台的目标特征的。比如它会检测你是不是有CC或GCC,并不是需要CC或GCC)

在这里插入图片描述

编译
    #  make(是用来编译的,它从Makefile中读取指令,然后编译。)
    安装
    #  make install(用来安装的,它也从Makefile中读取指令,安装到指定的位置。)
    检查是否安装
     pcre-config --version
nginx安装
   配置
   #./configure

在这里插入图片描述

编译
    #make
    安装
     #  make install
  安装目录
    /usr/local/ngnix
    启动:(进入上面红圈安装的目录,在sbin中,有nginx的启动文件)
    ./nginx
   (如果报错 error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory,用如下方法查看缺少)
      ldd $(which /usr/local/nginx/sbin/nginx)
      ldd是list, dynamic, dependencies的缩写, 意思是, 列出动态库依赖关系。

在这里插入图片描述
通过列表可以看出,在/lib64下缺少libpcre.so.1我们到这里面去建立软连接
ln -s libpcre.so.0.0.1 libpcre.so.1
然后再回去启动nginx
查看nginx进程是否启动: ps -ef|grep nginx
重启nginx : ./nginx -s reload

在安装目录的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 {
    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;
 ==================开始=====================
upstream my_server {                                                         
 server 192.168.23.112:8080;                                                  
    keepalive 2000;
}
 ==================结束=====================
    server {
        listen       80;
         ==================开始=====================
        server_name  localhost;
 ==================结束=====================
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
     ==================开始=====================
location /my/ {
        proxy_pass http://my_server/;
    }
     ==================结束=====================
        #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;
    #    }
    #}

}

在这里插入图片描述
启动反向代理服务器和web服务器,测试配置结果
参考帖子
https://baike.baidu.com/item/nginx/3817705?fr=aladdin
https://www.cnblogs.com/Anker/p/6056540.html
https://www.zhihu.com/question/24723688

猜你喜欢

转载自blog.csdn.net/weixin_44001965/article/details/86507020