Nginx搭建与配置

1 安装

1.1 安装前准备

1.1.1 安装JDK

1.1.2 安装yum

1.2 安装nginx依赖

1.2.1 使用root用户ssh登录服务器,以172.16.90.43为例

复制代码
ssh root@172.16.90.43

1.2.2 执行脚本安装依赖

复制代码
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
yum install -y gcc gcc-c++ zip unzip autoconf automake make

1.3 上传、解压、编译nginx

1.3.1 创建目录

复制代码
mkdir -p /opt/itmrl/nginx/nginx-1.12.2
mkdir -p /opt/itmrl/nginx/installPackage

1.3.2 切换到目录/opt/itmrl/nginx/installPackage

复制代码
cd /opt/itmrl/nginx/installPackage

1.3.2 将{上线包}\installPackages\nginx\nginx-1.12.2.tar.gz上传到当前目录

1.3.3 解压nginx-1.12.2.tar.gz

复制代码
tar -zxvf nginx-1.12.2.tar.gz

1.3.4 进入解压目录内,设置安装路径

复制代码
#非SSL配置
cd /opt/itmrl/nginx/installPackage/nginx-1.12.2
./configure --prefix=/opt/itmrl/nginx/nginx-1.12.2

#SSL配置
cd /opt/itmrl/nginx/installPackage/nginx-1.12.2
./configure --prefix=/opt/itmrl/nginx/nginx-1.12.2 --with-http_ssl_module

1.3.5 编译安装

复制代码
make && make install

1.3 nginx启动、关闭

复制代码
# 启动nginx
/opt/itmrl/nginx/nginx-1.12.2/sbin/nginx

# 关闭nginx
/opt/itmrl/nginx/nginx-1.12.2/sbin/nginx -s stop

# 重新加载配置文件
/opt/itmrl/nginx/nginx-1.12.2/sbin/nginx -s reload

1.4 防火墙设置

1.4.1 防火墙开放端口

复制代码
firewall-cmd --zone=public --add-port=80/tcp --permanent

1.4.2 防火墙重新载入

复制代码
firewall-cmd --reload

2 配置

2.1 修改nginx.conf配置

2.1.1 切换到/opt/itmrl/nginx/nginx-1.12.2/conf

复制代码
cd /opt/itmrl/nginx/nginx-1.12.2/conf

2.1.2 将{上线包}\installPackages\nginx\nginx.conf上传到当前目录覆盖掉原来的nginx.conf

上线包中nginx.conf详细内容如下:

复制代码
#user  nobody;
worker_processes  8; 

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

#pid        logs/nginx.pid;


events {
    worker_connections  8192; 
}


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;
    
    client_max_body_size 64M;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    gzip  on;
	gzip_min_length 1k;
	gzip_buffers 4 16k;
	gzip_http_version 1.1;
	gzip_comp_level 5;
	gzip_types text/xml text/plain text/css text/javascript application/x-javascript application/javascript application/css  application/xml application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;
	gzip_vary on;
	gzip_disable "MSIE [1-6]\.";
    
    upstream api_server {
    	server 172.16.90.188:7070 max_fails=2 fail_timeout=60s;
    	server 172.16.90.97:7070 max_fails=2 fail_timeout=60s;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
        location /api/ {
            proxy_set_header Host  $host;
	        proxy_set_header X-Real-IP $remote_addr;
	        proxy_set_header X-Forwarded $proxy_add_x_forwarded_for;
	        proxy_pass http://api_server/;
	        # 默认连接超时时间 若给某一台服务器转发请求时,达到默认超时时间未响应,则再向另一台服务器转发请求
	        proxy_connect_timeout 3;
        }
        
        location /xxx-web/ {
	        root   html;
	        autoindex on;
	    }

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

        #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;
    #    }
    #}

}

2.1.3 根据实际情况修改集群负载服务IP和端口即可

复制代码
upstream api_server {
	server 172.16.90.188:7070 max_fails=2 fail_timeout=60s;
	server 172.16.90.97:7070 max_fails=2 fail_timeout=60s;
}

2.1.4 重新加载配置文

复制代码
/opt/itmrl/nginx/nginx-1.12.2/sbin/nginx -s reload

3 其他

复制代码
#隐藏版本号
server_tokens on;

#优化服务器域名的散列表大小 
server_names_hash_bucket_size 64;
server_names_hash_max_size 2048;

#开启高效文件传输模式
sendfile on;
#减少网络报文段数量
#tcp_nopush on;
#提高I/O性能
tcp_nodelay on;

#连接超时 时间定义 默认秒 默认65秒
keepalive_timeout 60;

#读取客户端请求头数据的超时时间 默认秒 默认60秒
client_header_timeout 15;

#读取客户端请求主体的超时时间 默认秒 默认60秒
client_body_timeout 15;

#响应客户端的超时时间 默认秒 默认60秒
send_timeout 25;

#上传文件的大小限制  默认1m
client_max_body_size 8m;

参考资料

nginx服务器之负载均衡策略

相关推荐
小熊officer2 小时前
Nginx中正向代理,反向代理,负载均衡
java·nginx·负载均衡
tzhou644522 小时前
Nginx 性能优化与防盗链配置
运维·nginx·性能优化
原神启动13 小时前
云计算大数据——Nginx 实战系列(性能优化与防盗链配置)
大数据·nginx·云计算
toooooop83 小时前
CentOS 7 系统上安装 **Nginx + MySQL 5.7 + PHP 7.3 + Redis** 的完整步骤
redis·mysql·nginx·centos·php7
k***45996 小时前
服务器无故nginx异常关闭之kauditd0 kswapd0挖矿病毒 CPU占用200% 内存耗尽
运维·服务器·nginx
记得记得就1517 小时前
【Nginx 实战系列(一)—— Web 核心概念、HTTP/HTTPS协议 与 Nginx 安装】
前端·nginx·http
6***B488 小时前
Nginx搭建负载均衡
运维·nginx·负载均衡
2501_939909058 小时前
Web与Nginx网站服务(1)
运维·nginx
m0_4887776517 小时前
Nginx虚拟主机
nginx·nginx虚拟主机