Nginx服务器简单配置

目录

[TOC]

一 安装Nginx

  • 1.8安装失败,下的1.14来安装的

1.1 安装步骤

参考文档: https://www.cnblogs.com/dtiove/p/5924385.html

  1. 下载源码后解压
  2. 进入解压后的目录,输入如下语句

    /usr/sbin/nginxsudo ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock

    niginx.conf地址为: /etc/nginx/nginx.conf

  3. 安装

    make && make install

二 Nginx操作方式

2.1 启动Nginx

  • 进入nginx所在根目录
    windows:
  • 执行nginx.exe或start nginx

linux:

  • ./nginx

2.2 停止nginx

  • nginx -s stop 或 nginx -s quit

2.3 查看nginx版本

  • nginx -v 或 nginx -V

2.4 修改配置文件后重载入

  • nginx -s reload(不用手动重启nginx)

2.5 测试当前配置文件是否正确

  • nginx -t

三 Nginx反向代理配置(配置nginx.conf)

3.1 操作步骤

  1. 备份nginx.conf(文件目录查看安装时的设置)
  2. 修改nginx.conf
  • 在http字典下设置upstream,定义该upstream的负载均衡算法以及各主机

    # 在http下设置upstrean字段,在该字段下,设置的server值为该集群下的tomcat主
    机的ip地址和端口号
    # 注意:设置了upstream之后需要在location字段再设置添加上http://upstream的
    名字,这个upstream的设置才能使用起来
    upstream test_gupan{
        ip_hash; # 需要设置的负载均衡算法,其他的还有轮询等
            server 172.16.225.1:8080;
            server 172.16.225.129:8080;
    }
  • 在http下的server字典下,找到location的设置,将设置的upstream添加至location下

        location / {
            # root   html;
            # index  index.html index.htm;
            # 这里要在proxy_pass配置好设置的userstream,才能实现负载均衡
            proxy_pass http://test_gupan;
        }
  • 设置保持连接时间,一般为65~120ms

        keepalive_timeout  65; # 保持连接的时间,这个值不要设置的太大,设置为65或120(>毫秒级)即可
  1. 启动nginx

启动时可能会报错(2018/06/25 09:43:18 [error] 4694#0: open() "/var/run/nginx/nginx.pid" failed (2: No such file or directory)),意思是/var/run/nginx/nginx.pid找不到,nginx.pid的路径为config文件中的第9行设置的文件路径,解决办法如下:
1. sudo nginx -c nginx配置文件路径(该路径查看安装时的设置)
2. sudo nginx s reload(重新载入配置文件)

  1. nginx负载均衡效果

3.2 nginx.conf配置文件设置案例


#user  nobody;
worker_processes  2; # 查看/proc/cpuinfo里面processor的最多的数目

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 删除pid这一行的注释,不然启动nginx会报错
pid        /var/run/nginx/nginx.pid;


events {
    worker_connections  1024; #设置最大连接数,设置为2时是1024,设置为4时是2048,以此类推
}


http {
    # 设置
        # 在http下设置upstrean字段,在该字段下,设置的server值为该集群下的tomcat主机的ip地址和端口号
        # 注意:设置了upstream之后需要在location字段再设置添加上http://upstream的名字,这个upstream的设置才能使用起来
    upstream test_gupan{
        ip_hash; # 需要设置的负载均衡算法,其他的还有轮询等
            server 172.16.225.1:8080;
            server 172.16.225.129:8080;
    }
    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; # 保持连接的时间,这个值不要设置的太大,设置为65或120(毫秒级)即可
    
    # gzip这个字段的设置默认是关闭的,但是这个字段设置了可以帮助我们帮图片等资源文件进行压缩,传送到后端服务器,节省带宽.
    gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # root   html;
            # index  index.html index.htm;
            # 这里要在proxy_pass配置好设置的userstream,才能实现负载均衡
            proxy_pass http://test_gupan;
        }
       

        # 错误页面输出等 
        #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;
    #    }
    #}

}

3.3 设置完成之后负载均衡测试

  • 输入-nginx -s reload重新加载配置文件
  • 在浏览器地址栏输入配置的nginx的IP地址,分别关闭ip地址为172.16.225.1:8080和172.16.225.129:8080的主机,看nginx的跳转
  • 当关闭172.16.225.1主机后,访问nginx的配置的ip地址后,访问的是172.16.225.129:8080的主机
  • 当关闭172.16.225.129主机后,访问nginx的配置的IP地址后,访问的是172.16.225.1:8080的主机

四 Nginx配置文件再多看看

  • nginx反向代理的指令不需要新增额外的模块,默认自带proxv_pass指令,只需要修改配置文件就可以实现反向代理

    upstream myserver{
    server 192.168.1.116 down; # down表示当前的server不参与负载
    server 192.168.1.117 backup; # backup表示其他所有的非backup机器比较忙的时候,请求abckup机器,所以这台机器的压力会最轻
    
    # weight参数表示权值,权值默认为1,权值越高被分配到的几率越大(所占几率是自身权值与所有权值总和的比值)
    server 192.168.1.121 weight=1;
    server 192.168.1.122 weight=2;
    }

猜你喜欢

转载自www.cnblogs.com/gupan/p/9227471.html