Nginx配置

nginx配置文件

主配置文件结构,四部

1、man block :主配置段,即全局配置段,对http,mail,tcp/udp,stream都有效

        event {

            ……

        }   事件驱动相关的配置

2、http  {

      ……

   }  http、https协议相关段配置文件

3、mail  {

      ……

  }  mail协议相关配置文件

4、stream  {

      ……

  }  stream服务器相关配置段

http相关配置结构

http   {

    ……

    ……各server的公共配置

    server{  每个server用于定义一个虚拟主机

      ……

      }

    server{

      ……

      server_name  虚拟主机名

      root  主目录

      alias  路径别名

      location [operator]  URL {  指定URL的特性

        ……

        if CONDITION {

          }

        }

      }

}

Nginx服务器

1.安装依赖包(需要连接网络或者通过系统镜像ISO文件安装)

yum -y install pcre-devel zlib-devel gcc gcc-c++ make

2.创建管理用户nginx

useradd -M -s /sbin/nologin nginx

3.解压nginx,并进入解压后nginx目录

tar xzvf nginx-1.6.0.tar.gz -C /opt
cd /opt/nginx-1.6.0/

4.配置

./configure
--prefix=/usr/local/nginx
--user=nginx
--group=nginx
--with-http_stub_status_module //开启stub_status状态统计模块//

5.编译及安装

make && make install

6.nginx连接至系统命令区,方便命令使用

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

7.创建nginx管理脚本

vi /etc/init.d/nginx

#!/bin/bash
#chkconfig: - 99 20
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

8.为nginx赋予执行权限,并加入系统服务管理

chmod +x /etc/init.d/nginx
chkconfig --add nginx

三、配置nginx负载均衡集群

修改nginx配置文件

vim /usr/local/nginx/conf/nginx.conf
#http{}标签内添加以下命令
upstream tomcat_server {
server 192.168.100.6:8080 weight=1;
server 192.168.100.7:8080 weight=1;
}
#在location / {}标签内添加
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat_server; #通过proxy_pass方法进行代理至tomcat_server的服务器组,其中http://不能省略
}

2.检查nginx配置

nginx -t

3.重启nginx服务

killall -1 nginx
#关闭selinux
setenforce 0
#关闭防火墙
systemctl stop firewalld.service

测试

客户机访问Nginx服务器IP地址:http://192.168.100.25/,通过不断的刷新浏览器测试,可以看到哦由于权重相同,页面在两个tomcat站点反复切换,这样说明负载均衡集群搭建成功了。

nginx主配置文件说明 nginx.conf

[root@b nginx]# cat nginx.conf
user nginx;          #运行nginx进程的系统用户
worker_processes auto;    #运行nginx的工作核心数
error_log /var/log/nginx/error.log;   #nginx错误日志
pid /run/nginx.pid;       #运行nginx的进程pid
include /usr/share/nginx/modules/*.conf;   #nginx包含的模块配置文件
events {
worker_connections 1024;    #每个nginx进程默认系统连接数
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

原文地址:https://www.cnblogs.com/xiaofeng666/p/10808047.html