首页 > 技术文章 > nginx_安装测试

shaozhiqi 2018-04-03 15:13 原文

 

首先安装环境: 

[root@local nginx-1.9.14]#  yum install gcc-c++  pcre pcre-devel  zlib zlib-devel openssl openssl--devel –y

安装之前,最好检查一下是否已经安装有nginx

 $   find -name nginx 

如果系统已经安装了nginx,那么就先卸载

$   yum remove nginx 
$   cd /usr/local 

从官网下载最新版的nginx

  $   wget http://nginx.org/download/nginx-1.7.4.tar.gz // wget     http://nginx.org/download/nginx-1.8.1.tar.gz

  

解压nginx压缩包

   $   tar -zxvf nginx-1.7.4.tar.gz 

会产生一个nginx-1.7.4 目录,这时进入nginx-1.7.4目录

$   cd  nginx-1.7.4 

接下来安装,使用--prefix参数指定nginx安装的目录,make、make install安装

 $  ./configure --prefix=/usr/local/nginx            //默认安装在/usr/local/nginx  
 $   make 
 $   make install    

启动nginx# /usr/local/nginx/sbin/nginx

停止nginx# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`

重启nginxkill -HUP `cat /usr/local/nginx/logs/nginx.pid`

添加到自启动# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local

chmod a+x  /etc/rc.local    执行这个才能设置成功

如果没有报错,顺利完成后,最好看一下nginx的安装目录

 配置负载均衡

 

修改配置文件

[root@local html]# vi /usr/local/nginx/conf/nginx.conf
[root@local html]# cat /usr/local/nginx/conf/nginx.conf
#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  10240;
}


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 mysvr {
        #weigth参数表示权值,权值越高被分配到的几率越大
        server 16.158.51.206:80 weight=5;
        server 16.158.51.51:80 weight=5;
        #server 192.168.207.131:8080 weight=2;
    }


    server {
        listen       80;
        server_name  16.158.51.2;
        index index.html;
        root /data0/htdocs/www;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # location / {
        #     root   html;
        #     index  index.html index.htm;
        # }


        #对 "/" 启用负载均衡
        location / {
        proxy_pass http://mysvr;  #以这种格式来使用后端的web服务器
        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;
        }
        #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;
        #    }
        #}
}


[root@vincentdembp html]#

 

启动ngnix

[root@local html]# /usr/local/nginx/sbin/nginx

重启nginx

[root@local html]# /usr/local/nginx/sbin/nginx -s reload
$ whereis nginx

  

 

配置后台服务

upstream mysvr {   
        #weigth参数表示权值,权值越高被分配到的几率越大  
        server 16.158.51.206:8098 weight=5;   
        server 16.158.51.206:8097 weight=5;   
        #server 192.168.207.131:8080 weight=2; 

     }  

  

访问测试:

@Test
public void  testqueryCompAndOrders(){
    String url="http://16.158.51.2/wos/comp/sendDetail";
    Map map=new HashMap<String, String>();
    map.put("reqId","\'C4012016042214145900001\'");
    postHttpRequestParams(url, map.toString());
}

关闭防火墙

systemctl stop firewalld
设置开启不启动:systemctl disable  firewalld

 

 

推荐阅读