Linux下Nginx1.8安装

来源:互联网 发布:单片机堆栈在哪里 编辑:程序博客网 时间:2024/05/17 00:49

摘要

LinuxNginx1.8安装


安装pcre

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz

tar -zxvf pcre-8.37.tar.gz

cd pcre-8.37

./configure

make

make install


安装openssl

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz

tar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c/

 

./config

make

make install

 

 

安装zlib         

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz

cd zlib-1.2.8

./configure

make

make install

 

 

下载Nginx1.8/usr/local,看命令吧:

 

$ tar xzf nginx-1.8.0.tar.gz

$ ./configure --prefix=/home/nginx/nginx

$ make

$ make install

若出现cc找不到执行

 

 

 

 yum -y install gcc automake autoconf libtool make

若启动出现如下错误

 

 error while loading shared libraries: libpcre.so.1: cannot open shared object file:

No such file or directory

执行以下命令

 

ln -s /usr/local/lib/libpcre.so.1 /lib64

安装完成之后生成一个nginx的文件夹,cd进去并切换到sbin目录

 

启动命令

 

$ ./nginx

关闭命令:

 

$ ./nginx -s stop

优雅重启

 

$ ./nginx -s reload

下面是配置:    

 

1、反向代理配置

 

修改部署目录下conf子目录的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)内容,可调整相关配置。

 

反向代理配置示例:

 

location / {

    #设置主机头和客户端真实地址,以便服务器获取客户端真实IP

     proxy_set_header Host $host;

     proxy_set_header X-Real-IP $remote_addr;

     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

     #禁用缓存

     proxy_buffering off;

     #设置反向代理的地址

     proxy_pass http://192.168.1.1;

}

代理地址根据实际情况修改。

 

2、负载均衡配置

 

nginx upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

 

另外一种方式是ip_hash:每个请求按访问iphash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

 

负载均衡配置示例:

 

upstream test{

     #ip_hash;

     server 192.168.1.251;

     server 192.168.1.252;

     server 192.168.1.247;

 }

server {

    listen       80;

    server_name  helloword;

    location / {

         #反向代理的地址

         proxy_pass http://test;     

    }

}

Upstream命名和服务器地址根据实际情况修改。

 

3、负载均衡+反向代理完整配置示例

 

nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream test{

         #ip_hash;

         server 192.168.1.251;

         server 192.168.1.252;

         server 192.168.1.247;

     }

    server {

        listen       80;

        server_name  2;

        location / {

        #设置主机头和客户端真实地址,以便服务器获取客户端真实IP

             proxy_set_header Host $host;

             proxy_set_header X-Real-IP $remote_addr;

             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

             #禁用缓存

             proxy_buffering off;

             #反向代理的地址

             proxy_pass http://test;   

        }

    }

}

4、动静分离配置    

 

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream test {

         #ip_hash;

         server 192.168.1.251;

         server 192.168.1.252;

         server 192.168.1.247;

     }

    server {

        listen       80;

        server_name  2;

        #配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。        

        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {

          root /usr/local/nginx/html/myloan;

          #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力

          expires      7d;

        }

         #所有jspdo的动态请求都交给后面的tomcat处理

         location ~ (\.jsp)|(\.do)$ {

              #tomcat地址

              proxy_pass http://test;   

              proxy_redirect off;  

              proxy_set_header HOST $host;  

              proxy_set_header X-Real-IP $remote_addr;  

              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  

              client_max_body_size 10m;  

              client_body_buffer_size 128k;  

              proxy_connect_timeout 90;  

              proxy_send_timeout 90;  

              proxy_read_timeout 90;  

              proxy_buffer_size 4k;  

              proxy_buffers 4 32k;  

              proxy_busy_buffers_size 64k;  

              proxy_temp_file_write_size 64k;  

        }      

    }

}

附录:nginx.conf配置详解

 

#运行用户

user www-data;    

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

 

#全局错误日志及PID文件

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

 

#工作模式及连接数上限

events {

    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

    worker_connections  1024;#单个后台worker process进程的最大并发链接数

    # multi_accept on;

}

 

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    #设定日志格式

    access_log    /var/log/nginx/access.log;

 

    #sendfile 指令指定 nginx是否调用 sendfile函数(zero copy方式)来输出文件,对于普通应用,

    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

    sendfile        on;

    #tcp_nopush     on;

 

    #连接超时时间

    #keepalive_timeout  0;

    keepalive_timeout  65;

    tcp_nodelay        on;

    

    #开启gzip压缩

    gzip  on;

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

 

    #设定请求缓冲

    client_header_buffer_size    1k;

    large_client_header_buffers  4 4k;

 

    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;

 

    #设定负载均衡的服务器列表

     upstream mysvr {

    #weigth参数表示权值,权值越高被分配到的几率越大

    #本机上的Squid开启3128端口

    server 192.168.8.1:3128 weight=5;

    server 192.168.8.2:80  weight=1;

    server 192.168.8.3:80  weight=6;

    }

 

 

   server {

    #侦听80端口

        listen       80;

        #定义使用www.xx.com访问

        server_name  www.xx.com;

 

        #设定本虚拟主机的访问日志

        access_log  logs/www.xx.com.access.log  main;

 

    #默认请求

    location / {

          root   /root;      #定义服务器的默认网站根目录位置

          index index.php index.html index.htm;   #定义首页索引文件的名称

 

          fastcgi_pass  www.xx.com;

         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;

          include /etc/nginx/fastcgi_params;

        }

 

    # 定义错误提示页面

    error_page   500 502 503 504 /50x.html;  

        location = /50x.html {

        root   /root;

    }

 

    #静态文件,nginx自己处理

    location ~ ^/(images|javascript|js|css|flash|media|static)/ {

        root /var/www/virtual/htdocs;

        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。

        expires 30d;

    }

    #PHP 脚本请求全部转发到 FastCGI处理.使用FastCGI默认配置.

    location ~ \.php$ {

        root /root;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;

        include fastcgi_params;

    }

    #设定查看Nginx状态的地址

    location /NginxStatus {

        stub_status            on;

        access_log              on;

        auth_basic              "NginxStatus";

        auth_basic_user_file  conf/htpasswd;

    }

    #禁止访问 .htxxx文件

    location ~ /\.ht {

        deny all;

    }

     

     }

}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果手机icloud满了怎么办 百度网盘密码忘记怎么办 手机卡不用了支付宝账号怎么办 手机校园网不弹出登录界面怎么办 百度云需要邮箱验证怎么办 网页百度网盘不播放视频怎么办 手机qq邮箱中转站容量不足怎么办 qq邮箱容量满了怎么办 手机邮箱中转站容量不足怎么办 微信号登陆要好友验证怎么办 邮箱账号名忘了怎么办 苹果六话筒坏了怎么办 iphone后置摄像头坏了怎么办 苹果8听歌声音小怎么办 酷狗音乐没了怎么办 手机听歌音量小怎么办 苹果手机酷狗音乐打不开怎么办 电脑酷狗音乐打不开怎么办 酷狗音乐无法运行播放怎么办? 酷狗音乐停止运行怎么办 米6蓝牙声音小怎么办 手里酷狗id丢了怎么办 连麦声音不好听怎么办 微信语音声音很难听怎么办 微信聊天语音没有声音怎么办 微信发语音声音不好听怎么办 微信语音说话不好听怎么办 微信字体变粗怎么办 笔记本无法识别usb设备怎么办 手机qq音乐闪退怎么办 qq音乐总是闪退怎么办 手机qq音乐闪退怎么办修复 苹果7p耳机漏音怎么办 akgn25耳机盖掉了怎么办 外汇平台跑路了怎么办 微云资料没了怎么办 酷狗k歌有杂音怎么办 手机k歌音质不好怎么办 酷狗让升级内测取消之后怎么办 苹果5s声音太小怎么办 苹果6p调均衡卡怎么办