Linux Java 服务器搭建-安装nginx(六)

1、gzip模块需要 zlib 库(Gzip压缩)
2、rewrite模块需要 pcre 库(使用正则对请求重写)
3、ssl 功能需要openssl库(HTTPS/SSL)

 

一、安装编译工具及库文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

 

 

二、要安装 PCRE

PCRE 作用是让 Nginx 支持 Rewrite 功能
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar -zxvf pcre-8.40.tar.gz
cd pcre-8.40
./configure && make && make install
查看pcre版本
[root@bogon pcre-8.40]# pcre-config --version
 

 

 

三、要安装 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

 

 

 

 

四、要安装 openssl

$ cd /usr/local/
$ wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz
$ tar -zxvf openssl-1.0.1j.tar.gz
$ ./config
$ make
$ make install
openssl version -a

 

 

 

 

 

 

 

 

安装nginx

#下载nginx源码包,并解压

wget http://nginx.org/download/nginx-1.10.2.tar.gz
cd /usr/local/nginx 
tar zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2

#设置参数 参数具体参考《Nginx编译参数》

# ./configure --prefix=/usr/local/nginx  

#编译并安装 

]# make  
[root@iZ11y6td7sjZ nginx-1.10.0]# make install  

 

 

 

 

 

 

启动nginx

cd /usr/local/nginx/sbin;输入./nginx
如果nginx被配置成了服务可以这样启动/etc/init.d/nginx start
一般这个时候会报错,不要慌张!!!
错误信息:error while loading shared libraries:libpcre.so.1.......
解决方案:
[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so.1 /usr/local/lib/libpcre.so
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx 
[root@localhost nginx]# ps -aux | grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root    12007  0.0  0.0  20296   628 ?        Ss   13:28   0:00 nginx: master process sbin/nginx
nobody    12008  0.0  0.1  20716  1220 ?        S    13:28   0:00 nginx: worker process
root      12010  0.0  0.0 103244   836 pts/0    S+   13:29   0:00 grep nginx
测试安装是否成功:
进入linux自带的火狐浏览器,键入localhost(ip),会跳出welcome to nginx!(切忌关闭防火墙),如果你一开始开放80端口就没问题

 

 

Nginx 其他命令

以下包含了 Nginx 常用的几个命令:
cd /usr/local/nginx/sbin/nginx -s reload            # 重新载入配置文件
cd /usr/local/nginx/sbin/nginx -s reopen            # 重启 Nginx
cd /usr/local/nginx/sbin/nginx -s stop              # 停止 Nginx
css,js无法加载




proxy_set_header        Host $host; #从header头中获取的主机名
proxy_set_header        X-Real-IP $remote_addr; #获取header头中获取的主机的真实IP
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取header头中获取代理者的真实ip

 

 

 

 

Nginx配置文件nginx.conf详解

 配置文件主要由四部分组成:main(全区设置),server(主机配置),upstream(负载均衡服务器设置),和location(URL匹配特定位置设置)。

   1)全局变量

  1. #Nginx的worker进程运行用户以及用户组
  2. #user  nobody nobody;
  3. #Nginx开启的进程数
  4. worker_processes  1;
  5. #worker_processes auto;
  6. #以下参数指定了哪个cpu分配给哪个进程,一般来说不用特殊指定。如果一定要设的话,用0和1指定分配方式.
  7. #这样设就是给1-4个进程分配单独的核来运行,出现第5个进程是就是随机分配了。eg:
  8. #worker_processes 4     #4核CPU
  9. #worker_cpu_affinity 0001 0010 0100 1000
  10.  
  11.  
  12. #定义全局错误日志定义类型,[debug|info|notice|warn|crit]
  13. #error_log  logs/error.log  info;
  14. #指定进程ID存储文件位置
  15. #pid        logs/nginx.pid;
  16. #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n的值保持一致。
  17. #vim /etc/security/limits.conf
  18. #  *                soft    nproc          65535
  19. #  *                hard    nproc          65535
  20. #  *                soft    nofile         65535
  21. #  *                hard    nofile         65535
  22. worker_rlimit_nofile 65535;

    2)事件配置

  1. events {
  2.     #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  3.     use epoll;
  4.     #每个进程可以处理的最大连接数,理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。理论值:worker_rlimit_nofile/worker_processes
  5.     #注意:最大客户数也由系统的可用socket连接数限制(~ 64K),所以设置不切实际的高没什么好处
  6.     worker_connections  65535;   
  7.     #worker工作方式:串行(一定程度降低负载,但服务器吞吐量大时,关闭使用并行方式)
  8.     #multi_accept on;
  9. }

   3)http参数

  1.     #文件扩展名与文件类型映射表
  2.     include mime.types;
  3.     #默认文件类型
  4.     default_type application/octet-stream;
  5. #日志相关定义
  6.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  7.     #                  '$status $body_bytes_sent "$http_referer" '
  8.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  9.     #定义日志的格式。后面定义要输出的内容。
  10.     #1.$remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
  11.     #2.$remote_user :用来记录客户端用户名称;
  12.     #3.$time_local :用来记录访问时间与时区;
  13.     #4.$request  :用来记录请求的url与http协议;
  14.     #5.$status :用来记录请求状态;
  15.     #6.$body_bytes_sent :记录发送给客户端文件主体内容大小;
  16.     #7.$http_referer :用来记录从那个页面链接访问过来的;
  17.     #8.$http_user_agent :记录客户端浏览器的相关信息
  18.     #连接日志的路径,指定的日志格式放在最后。
  19.     #access_log  logs/access.log  main;
  20.     #只记录更为严重的错误日志,减少IO压力
  21.     error_log logs/error.log crit;
  22.     #关闭日志
  23.     #access_log  off;
  24.  
  25.     #默认编码
  26.     #charset utf-8;
  27.     #服务器名字的hash表大小
  28.     server_names_hash_bucket_size 128;
  29.     #客户端请求单个文件的最大字节数
  30.     client_max_body_size 8m;
  31.     #指定来自客户端请求头的hearerbuffer大小
  32.     client_header_buffer_size 32k;
  33.     #指定客户端请求中较大的消息头的缓存最大数量和大小。
  34.     large_client_header_buffers 4 64k;
  35.     #开启高效传输模式。
  36.     sendfile        on;
  37.     #防止网络阻塞
  38.     tcp_nopush on;
  39.     tcp_nodelay on;    
  40.     #客户端连接超时时间,单位是秒
  41.     keepalive_timeout 60;
  42.     #客户端请求头读取超时时间
  43.   client_header_timeout 10; 
  44. #设置客户端请求主体读取超时时间
  45.     client_body_timeout 10; 
  46.     #响应客户端超时时间 
  47.     send_timeout 10;
  48.  
  49. #FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。
  50.     fastcgi_connect_timeout 300;
  51.     fastcgi_send_timeout 300;
  52.     fastcgi_read_timeout 300;
  53.     fastcgi_buffer_size 64k;
  54.     fastcgi_buffers 4 64k;
  55.     fastcgi_busy_buffers_size 128k;
  56.     fastcgi_temp_file_write_size 128k;
  57.  
  58. #gzip模块设置
  59.     #开启gzip压缩输出
  60.     gzip on;
  61.     #最小压缩文件大小
  62.     gzip_min_length 1k;
  63.     #压缩缓冲区
  64.     gzip_buffers 4 16k;
  65.     #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
  66.     gzip_http_version 1.0;
  67.     #压缩等级 1-9 等级越高,压缩效果越好,节约宽带,但CPU消耗大
  68.     gzip_comp_level 2;
  69.     #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。
  70.     gzip_types text/plain application/x-javascript text/css application/xml;
  71.     #前端缓存服务器缓存经过压缩的页面 
  72.     gzip_vary on;

    4)虚拟主机基本设置

  1. #虚拟主机定义
  2.     server {
  3.         #监听端口
  4.         listen       80;
  5.        #访问域名 
  6.         server_name  localhost;
  7.        #编码格式,若网页格式与此不同,将被自动转码 
  8.         #charset koi8-r;
  9.       #虚拟主机访问日志定义 
  10.         #access_log  logs/host.access.log  main;
  11.         #对URL进行匹配 
  12.         location / {
  13.                  #访问路径,可相对也可绝对路径 
  14.             root   html;
  15.                   #首页文件。以下按顺序匹配
  16.             index  index.html index.htm;
  17.         }
  18.   
  19. #错误信息返回页面
  20.         #error_page  404              /404.html;
  21.         # redirect server error pages to the static page /50x.html
  22.         #
  23.         error_page   500 502 503 504  /50x.html;
  24.         location = /50x.html {
  25.             root   html;
  26.         }
  27.  
    1. #访问URL以.php结尾则自动转交给127.0.0.1
  28.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  29.         #
  30.         #location ~ \.php$ {
  31.         #    proxy_pass   http://127.0.0.1;
  32.         #}
  33. #php脚本请求全部转发给FastCGI处理
  34.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  35.         #
  36.         #location ~ \.php$ {
  37.         #    root           html;
  38.         #    fastcgi_pass   127.0.0.1:9000;
  39.         #    fastcgi_index  index.php;
  40.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  41.         #    include        fastcgi_params;
  42.         #}
  43.  
  44. #禁止访问.ht页面 (需ngx_http_access_module模块)
  45.         # deny access to .htaccess files, if Apache's document root
  46.         # concurs with nginx's one
  47.         #location ~ /\.ht {
  48.         #    deny  all;
  49.         #}
  50.     }
  51. #HTTPS虚拟主机定义
  52.     # HTTPS server
  53.     #
  54.     #server {
  55.     #    listen       443 ssl;
  56.     #    server_name  localhost;
  57.     #    ssl_certificate      cert.pem;
  58.     #    ssl_certificate_key  cert.key;
  59.     #    ssl_session_cache    shared:SSL:1m;
  60.     #    ssl_session_timeout  5m;
  61.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  62.     #    ssl_prefer_server_ciphers  on;
  63.     #    location / {
  64.     #        root   html;
  65.     #        index  index.html index.htm;
  66.     #    }
  67.     #}

    5)Nignx状态监控

  1. #Nginx运行状态,StubStatus模块获取Nginx自启动的工作状态(编译时要开启对应功能)
  2.         #location /NginxStatus {
  3.         #    #启用StubStatus的工作访问状态    
  4.         #    stub_status    on;
  5.         #    #指定StubStaus模块的访问日志文件
  6.         #    access_log    logs/Nginxstatus.log;
  7.         #    #Nginx认证机制(需Apache的htpasswd命令生成)
  8.         #    #auth_basic    "NginxStatus";
  9.         #    #用来认证的密码文件
  10.         #    #auth_basic_user_file    ../htpasswd;    
  11.         #}
  12. 访问:http://IP/NginxStatus(测试就不加密码验证相关)

    6)反向代理

  1. #以下配置追加在HTTP的全局变量中
  2. #nginx跟后端服务器连接超时时间(代理连接超时)
  3. proxy_connect_timeout      5;
  4. #后端服务器数据回传时间(代理发送超时)
  5. proxy_send_timeout         5;
  6. #连接成功后,后端服务器响应时间(代理接收超时)
  7. proxy_read_timeout         60;
  8. #设置代理服务器(nginx)保存用户头信息的缓冲区大小
  9. proxy_buffer_size          16k;
  10. #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
  11. proxy_buffers              4 32k;
  12. #高负荷下缓冲大小(proxy_buffers*2)
  13. proxy_busy_buffers_size    64k;
  14. #设定缓存文件夹大小,大于这个值,将从upstream服务器传
  15. proxy_temp_file_write_size 64k;
  16. #反向代理缓存目录
  17. proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
  18. #levels=1:2 设置目录深度,第一层目录是1个字符,第2层是2个字符
  19. #keys_zone:设置web缓存名称和内存缓存空间大小
  20. #inactive:自动清除缓存文件时间。
  21. #max_size:硬盘空间最大可使用值。
  22. #指定临时缓存文件的存储路径(路径需和上面路径在同一分区)
  23. proxy_temp_path /data/proxy/temp
  24.  
  25. #服务配置
  26. server {
  27.     #侦听的80端口
  28.     listen       80;
  29.     server_name  localhost;
  30.     location / {
  31.        #反向代理缓存设置命令(proxy_cache zone|off,默认关闭所以要设置)
  32.      proxy_cache cache_one;
  33.     #对不同的状态码缓存不同时间
  34.            proxy_cache_valid 200 304 12h;
  35.         #设置以什么样参数获取缓存文件名
  36.         proxy_cache_key $host$uri$is_args$args;
  37.         #后7端的Web服务器可以通过X-Forwarded-For获取用户真实IP
  38.         proxy_set_header Host $host;
  39.         proxy_set_header X-Real-IP $remote_addr;
  40.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
  41.         #代理设置
  42.         proxy_pass   http://IP;
  43.    #文件过期时间控制
  44.         expires    1d;
  45.     }
    1.     #配置手动清楚缓存(实现此功能需第三方模块 ngx_cache_purge)
    2.  
  46.     #http://www.123.com/2017/0316/17.html访问
    1.     #http://www.123.com/purge/2017/0316/17.html清楚URL缓存
    2.  
  47.     location ~ /purge(/.*) {
  48.         allow    127.0.0.1;
  49.         deny    all;
  50.         proxy_cache_purge    cache_one    $host$1$is_args$args;
  51.     }
  52.     #设置扩展名以.jsp、.php、.jspx结尾的动态应用程序不做缓存
  53.     location ~.*\.(jsp|php|jspx)?$ {
  54.         proxy_set_header Host $host;
  55.         proxy_set_header X-Real-IP $remote_addr;
  56.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
  57.         proxy_pass http://http://IP;
  58.     }

    7)负载均衡

  1. #负载均衡服务器池
  2. upstream my_server_pool {
  3.   #调度算法
  4.   #1.轮循(默认)(weight轮循权值) 
  5.    #2.ip_hash:根据每个请求访问IP的hash结果分配。(会话保持)
  6.     #3.fair:根据后端服务器响应时间最短请求。(upstream_fair模块)
  7.     #4.url_hash:根据访问的url的hash结果分配。(需hash软件包)
  8.     #参数: 
  9.    #down:表示不参与负载均衡 
  10.    #backup:备份服务器 
  11.     #max_fails:允许最大请求错误次数 
  12.     #fail_timeout:请求失败后暂停服务时间。
  13.     server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
  14.     server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
  15. }
  16. #负载均衡调用
  17. server {
  18.     ...
  19.     location / {
  20.     proxy_pass http://my_server_pool;
  21.    } 
  22. }

    8)URL重写

  1. #根据不同的浏览器URL重写
  2. if($http_user_agent ~ Firefox){
  3. rewrite ^(.*)$  /firefox/$1 break;
  4. }
  5. if($http_user_agent ~ MSIE){
  6. rewrite ^(.*)$  /msie/$1 break;
  7. }
  8.  
  9. #实现域名跳转
  10. location / {
  11.     rewrite ^/(.*)$ https://web8.example.com$1 permanent;
  12. }

    9)IP限制

  1. #限制IP访问
  2. location / {
  3.     deny 192.168.0.2;
  4.     allow 192.168.0.0/24;
  5.     allow 192.168.1.1;
  6.     deny all;
  7. }

    10)Nginx相关命令

  1. #启动nginx
  2. nginx
  3. #关闭nginx
  4. nginx -s stop
  5. #平滑重启
  6.  
  7. kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
  8.  

    11)Nginx启动脚本

  1. #!/bin/bash
  2. #chkconfig: 2345 80 90
  3. #description:auto_run
  4. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  5. export PATH
  6.  
  7. # Check if user is root
  8. if [ $(id -u) != "0" ]; then
  9.    echo "Error: You must be root to run this script!\n"
  10.    exit 1
  11. fi
  12.  
  13. NGINXDAEMON=/usr/local/nginx/sbin/nginx
  14. PIDFILE=/usr/local/nginx/logs/nginx.pid
  15.  
  16. function_start()
  17. {
  18.    echo -en "\033[32;49;1mStarting nginx......\n"
  19.    echo -en "\033[39;49;0m" 
  20.    if [ -f $PIDFILE ]; then
  21.      printf "Nginx is runing!\n"
  22.      exit 1
  23.    else 
  24.        $NGINXDAEMON
  25.        printf "Nginx is the successful start!\n"
  26.    fi
  27. }
  28.  
  29. function_stop()
  30. {
  31.    echo -en "\033[32;49;1mStoping nginx......\n"
  32.    echo -en "\033[39;49;0m"
  33.    if  [ -f $PIDFILE ]; then
  34.        kill `cat $PIDFILE`
  35.        printf "Nginx program is stoped\n"
  36.    else 
  37.        printf  "Nginx program is not runing!\n"
  38.    fi
  39. }
  40.  
  41. function_reload()
  42. {
  43.    echo -en "\033[32;49;1mReload nginx......\n"
  44.    echo -en "\033[39;49;0m"
  45.    function_stop
  46.    function_start
  47.  
  48. }
  49.  
  50. function_restart()
  51. {
  52.    echo -en "\033[32;49;1mRestart nginx......\n"
  53.    echo -en "\033[39;49;0m"
  54.    printf "Reload Nginx configure...\n"
  55.    $NGINXDAEMON -t
  56.    kill -HUP `cat $PIDFILE`
  57.    printf "Nginx program is reloding!\n"
  58. }
  59.  
  60. function_kill()
  61. {
  62.    killall nginx
  63. }
  64.  
  65. function_status()
  66. {
  67.    if ! ps -ef|grep -v grep|grep 'nginx:' > /dev/null 2>&1
  68.    then
  69.        printf "Nginx is down!!!\n"
  70.    else
  71.        printf "Nginx is running now!\n"
  72.    fi
  73. }
  74.  
  75. if [ "$1" = "start" ]; then
  76.    function_start
  77. elif [ "$1" = "stop" ]; then
  78.    function_stop
  79. elif [ "$1" = "reload" ]; then
  80.    function_reload
  81. elif [ "$1" = "restart" ]; then
  82.    function_restart
  83. elif [ "$1" = "kill" ]; then
  84.    function_kill
  85. elif [ "$1" = "status" ]; then
  86.    function_status
  87. else
  88.    echo -en "\033[32;49;1m Usage: nginx {start|stop|reload|restart|kill|status}\n"
  89.    echo -en "\033[39;49;0m"
  90. fi

 

 

配置负载均衡

    upstream mysvr {
#user  nobody;
worker_processes  auto;
#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;
proxy_set_header        Host $host; #从header头中获取的主机名
proxy_set_header        X-Real-IP $remote_addr; #获取header头中获取的主机的真实IP
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取header头中获取代理者的真实ip
#设定负载均衡的服务器列表
    upstream mysvr {
        #weigth参数表示权值,权值越高被分配到的几率越大
        #本机上的Squid开启3128端口
#  默认为1  weight越大,负载的权重就越大。
        server 192.168.15.150:8080 weight=2;
        server 192.168.15.150:9080;
    }
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm index.jsp;
    proxy_pass http://mysvr;
        }
#静态资源配置
#location ~ .*\.(html|gif|jpg|jpeg|png|bmp|swf|js|css)$
       # {
      #     proxy_pass http://192.168.15.150:8080;
      #  }
        #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;
        }
       
    }
    }

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值