centos7安装nginx

1.安装依赖

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
  • gcc-c++:安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境。 
  • pcre pcre-devel :PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正 则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上 安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。 
  • zlib zlib-devel:zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip    
  • openssl openssl-devel:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)

2.进入安装目录,下载nginx

#进入安装目录
cd /usr/local

#下载nginx
wget http://nginx.org/download/nginx-1.18.0.tar.gz

3.解压文件

tar -zxvf nginx-1.18.0.tar.gz 

#进入安装目录
cd nginx-1.18.0/

4.配置

./configure --prefix=/usr/local/nginx --with-http_ssl_module

5.编译安装nginx

make
make install

6.设置nginx为开机自启动

vi /etc/init.d/nginx
#!/bin/sh  
# chkconfig: 2345 85 15  
# Startup script for the nginx Web Server  
# description: nginx is a World Wide Web server.   
# It is used to serve HTML files and CGI.  
# processname: nginx  
# pidfile: /usr/local/nginx/logs/nginx.pid  
# config: /usr/local/nginx/conf/nginx.conf  
  
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
DESC="nginx deamon"  
NAME=nginx  
DAEMON=/usr/local/nginx/sbin/$NAME  
SCRIPTNAME=/etc/init.d/$NAME  
  
test -x $DAEMON || exit 0  
  
d_start(){  
  $DAEMON || echo -n "already running"  
}  
  
d_stop(){  
  $DAEMON -s quit || echo -n "not running"  
}  
  
  
d_reload(){  
  $DAEMON -s reload || echo -n "can not reload"  
}  
  
case "$1" in  
start)  
  echo -n "Starting $DESC: $NAME"  
  d_start  
  echo "."  
;;  
stop)  
  echo -n "Stopping $DESC: $NAME"  
  d_stop  
  echo "."  
;;  
reload)  
  echo -n "Reloading $DESC conf..."  
  d_reload  
  echo "reload ."  
;;  
restart)  
  echo -n "Restarting $DESC: $NAME"  
  d_stop  
  sleep 2  
  d_start  
  echo "."  
;;  
*)  
  echo "Usage: $ScRIPTNAME {start|stop|reload|restart}" >&2  
  exit 3  
;;  
esac  
  
exit 0  

保存退出后。

使用下面的命令,使其可执行

 chmod +x /etc/init.d/nginx 

然后,添加配置并查看

#添加nginx服务到自启动
chkconfig --add nginx 
#on是开启 off关闭
chkconfig nginx on/off  

7.修改nginx.conf

cd /usr/local/nginx/conf
vim nginx.conf
worker_processes  4;

events {
    worker_connections  65535;
}


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"';

   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  /home/log/nginx/access.log  main;
	
	server_names_hash_bucket_size 128; 
    client_header_buffer_size 32k; 
    large_client_header_buffers 4 64k; 


    sendfile        on;
    autoindex       on; 
    tcp_nopush      on; 
    tcp_nodelay     on;
    keepalive_timeout  65;

    gzip on; 
	gzip_min_length 1k; 
	gzip_buffers 4 16k; 
	gzip_http_version 1.1; 
	gzip_comp_level 2; 
	gzip_types text/plain application/x-javascript text/css application/xml;
	gzip_vary on;
	#limit_zone crawler $binary_remote_addr 10m; 

    client_max_body_size 300m; 
	client_body_buffer_size 128k;
	proxy_connect_timeout 600;
	proxy_read_timeout 600;
	proxy_send_timeout 600;
	proxy_buffer_size 16k;
	proxy_buffers 4 64k;
	proxy_busy_buffers_size 128k;
	proxy_temp_file_write_size 128k;
	proxy_temp_path /usr/local/nginx/proxy_temp;
	proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=tmpcache:200m inactive=1d max_size=2g;
	

    #配置文件加载
	include /usr/local/nginx/conf/conf.d/*.conf;

}

8.新建配置文件加载目录,对应上面conf的配置文件加载目录。后面可以将所有的配置文件放在该文件夹下面统一管理。

upstream.conf管理所有的解析

upstream proxy-bt {
	server IP:端口号 weight=5 max_fails=2 fail_timeout=30s;
	keepalive 20;
}

每个应用一个配置文件,例如宝塔应用对应bt.conf

server
{
        listen 80;
        server_name bt2.XXX.com;

        location /
        {
                proxy_pass http://proxy-bt/;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
        }


        #缓存静态资源
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|html|shtml|htm)$
        {
                proxy_cache tmpcache;
                proxy_cache_valid 200 304 12h;
                proxy_cache_valid 301 302 1m;
                proxy_cache_valid any 1m;
                proxy_cache_key $host$uri$is_args$args;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_pass http://proxy-bt;
        }
        #解析日志存放
        access_log /home/log/nginx/bt.log main;
}

 

9.nginx操作命令

相关操作命令
service nginx start

service nginx stop

service nginx reload

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值