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

1、gzip模块需要 zlib 库
2、rewrite模块需要 pcre 库
3、ssl 功能需要openssl库

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

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

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
# ./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


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

    upstream mysvr {
其他配置 https://blog.csdn.net/hzsunshine/article/details/63687054
配置负载均衡
#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;
        }
       
    }
    }

猜你喜欢

转载自blog.csdn.net/qq_39526250/article/details/80608850