Nginx安装文档

nginx安装:

	一、准备工作
	二、安装步骤
	三、注册服务
	四、防火墙配置
	五、nginx.conf文件配置详解

nginx安装

一、准备工作
1.下载地址:

1)https://nginx.org/en/download.html
2)wget https://nginx.org/download/nginx-1.18.0.tar.gz

2.安装vim、make、ifconfig等工具:

yum install -y vim make net-tools wget

3.安装依赖:

yum -y install gcc pcre-devel zlib-devel openssl openssl-devel

4.关闭selinux:

1) 进入目录: cd /etc/selinux
2) 编辑文件: vim config
3) 设置文档: SELINUX=disabled
4) 编译修改文件或重启系统

二、安装步骤

1.将下载好的文件传入到指定文件夹
2.进入该文件夹下并解压:

1) 进入目录: cd /home/nsiTeach --没有目录需要创建:mkdir nsiTeach
2) 解压文件: tar -zxvf nginx-1.18.0.tar.gz

3.进入解压后的文件夹:

 cd /home/nsiTeach/nginx-1.18.0/

4.隐藏nginx信息:

1) 进入目录: cd /home/nsiTeach/nginx-1.18.0/src/core/
1) 找到文件: nginx.h
3) 编辑文件: vim nginx.h
...
#define NGINX_VERSION "" -- 隐藏版本号
#define NGINX_VER "" NGINX_VERSION -- 隐藏服务名
...
#define NGINX_VAR "" --
...
  1. 返回目录:

     cd /home/nsiTeach/nginx-1.18.0/
    
  2. 配置nginx:

     ./configure --prefix=/home/nsiTeach/nginx [--withhttp_ssl_module 支持HTTPS协议]
    
    
     Configuration summary
     + using system PCRE library
     + OpenSSL library is not used
     + using system zlib library
     ...
    
  3. 编译安装:

     make && make install
    
  4. 进入目录:

     cd /home/nsiTeach/nginx
    
  5. 查看文件:

     client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
    
  6. 执行命令:

      ./sbin/nginx
    
  7. 验证是否安装成功

三、注册服务
1.进入目录:

 cd /lib/systemd/system/

2.创建文件:

 vim nginx.service

3.复制以下内容并保存退出:

[Unit]
Description=nginx - high performance web server --- 描述
Documentation=http://nginx.org/en/docs/ --- 文档地址
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/home/nsiTeach/nginx/logs/nginx.pid --- nginx.pid位置,一般在
logs下,可不写
ExecStartPre=/home/nsiTeach/nginx/sbin/nginx -t -c
/home/nsiTeach/nginx/conf/nginx.conf --- 启动前的准备
ExecStart=/home/nsiTeach/nginx/sbin/nginx -c
/home/nsiTeach/nginx/conf/nginx.conf --- nginx启动命令
ExecReload=/bin/kill -s HUP $MAINPID --- nginx重启
ExecStop=/bin/kill -s QUIT $MAINPID --- nginx停止
PrivateTmp=true
[Install]
WantedBy=multi-user.target

4.执行命令:

 systemctl daemon-reload

5.nginx命令:

systemctl start nginx.service
systemctl restart nginx.service
systemctl stop nginx.service

6.加入开机自启:

systemctl enable nginx.service

四、防火墙配置

iptables:
firewalld:
firewall-cmd --add-port=80/tcp --permanent
【参数详情见】:防火墙配置.pdf / 防火墙配置.md

五、nginx.conf文件配置详解
##代码块中的events、http、server、location、upstream等都是块配置项##

##块配置项可以嵌套。内层块直接继承外层快,例如:server块里的任意配置都是基于http块里的已有配置的##

##Nginx worker进程运行的用户及用户组

#语法:user username[groupname] 默认:user nobody nobody

#user用于设置master进程启动后,fork出的worker进程运行在那个用户和用户组下。当按照"user username;"设置时,用户组名与用户名相同。

#若用户在configure命令执行时,使用了参数–user=usergroup 和 --group=groupname,此时nginx.conf将使用参数中指定的用户和用户组。
#user nobody;

##Nginx worker进程个数:其数量直接影响性能。
#每个worker进程都是单线程的进程,他们会调用各个模块以实现多种多样的功能。如果这些模块不会出现阻塞式的调用,那么,有多少CPU内核就应该配置多少个进程,反之,有可能出现阻塞式调用,那么,需要配置稍多一些的worker进程。

worker_processes 1;

##ssl硬件加速。
#用户可以用OpneSSL提供的命令来查看是否有ssl硬件加速设备:openssl engine -t
#ssl_engine device;

##守护进程(daemon)。是脱离终端在后台允许的进程。它脱离终端是为了避免进程执行过程中的信息在任何终端上显示。这样一来,进程也不会被任何终端所产生的信息所打断。##
##关闭守护进程的模式,之所以提供这种模式,是为了放便跟踪调试nginx,毕竟用gdb调试进程时最繁琐的就是如何继续跟进fork出的子进程了。##
##如果用off关闭了master_proccess方式,就不会fork出worker子进程来处理请求,而是用master进程自身来处理请求
#daemon off; #查看是否以守护进程的方式运行Nginx 默认是on
#master_process off; #是否以master/worker方式工作 默认是on

##error日志的设置#
#语法: error_log /path/file level;
#默认: error_log / log/error.log error;
#当path/file 的值为 /dev/null时,这样就不会输出任何日志了,这也是关闭error日志的唯一手段;
#leve的取值范围是debug、info、notice、warn、error、crit、alert、emerg从左至右级别依次增大。
#当level的级别为error时,error、crit、alert、emerg级别的日志就都会输出。大于等于该级别会输出,小于该级别的不会输出。
#如果设定的日志级别是debug,则会输出所有的日志,这一数据量会很大,需要预先确保/path/file所在的磁盘有足够的磁盘空间。级别设定到debug,必须在configure时加入 --with-debug配置项。
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

##pid文件(master进程ID的pid文件存放路径)的路径
#pid logs/nginx.pid;

events {
#仅对指定的客户端输出debug级别的日志: 语法:debug_connection[IP|CIDR]
#这个设置项实际上属于事件类配置,因此必须放在events{……}中才会生效。它的值可以是IP地址或者是CIRD地址。
#debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24
#这样,仅仅以上IP地址的请求才会输出debug级别的日志,其他请求仍然沿用error_log中配置的日志级别。
#注意:在使用debug_connection前,需确保在执行configure时已经加入了–with-debug参数,否则不会生效。
worker_connections 1024;
}

##核心转储(coredump):在Linux系统中,当进程发生错误或收到信号而终止时,系统会将进程执行时的内存内容(核心映像)写入一个文件(core文件),以作为调试只用,这就是所谓的核心转储(coredump).

http {
##嵌入其他配置文件 语法:include /path/file
#参数既可以是绝对路径也可以是相对路径(相对于Nginx的配置目录,即nginx.conf所在的目录)
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 bakend {
		server 127.0.0.1:8027;

		server 127.0.0.1:8028;

		server 127.0.0.1:8029;

		hash $request_uri;

	}

	nginx的upstream目前支持4种方式的分配

	1、轮询(默认)

	每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

	2、weight
	指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
	例如:
	upstream bakend {
		server 192.168.0.14 weight=10;
		server 192.168.0.15 weight=10;
	}

	2、ip_hash
	每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
	例如:
	upstream bakend {
		ip_hash;
		server 192.168.0.14:88;
		server 192.168.0.15:80;
	}

	3、fair(第三方)
	按后端服务器的响应时间来分配请求,响应时间短的优先分配。
	upstream backend {
		server server1;
		server server2;
		fair;
	}

	4、url_hash(第三方)

	按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

	例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法

	upstream backend {
		server squid1:3128;
		server squid2:3128;
		hash $request_uri;
		hash_method crc32;
	}

	tips:

	upstream bakend{#定义负载均衡设备的Ip及设备状态}{
		ip_hash;
		server 127.0.0.1:9090 down;
		server 127.0.0.1:8080 weight=2;
		server 127.0.0.1:6060;
		server 127.0.0.1:7070 backup;
	}
	在需要使用负载均衡的server中增加
	proxy_pass http://bakend/;

	每个设备的状态设置为:
	1.down表示单前的server暂时不参与负载
	2.weight为weight越大,负载的权重就越大。
	3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
	4.fail_timeout:max_fails次失败后,暂停的时间。
	5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

	nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

	client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
	client_body_temp_path设置记录文件的目录 可以设置最多3层目录

	location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

	 

server {

##listen监听的端口
#语法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ]
#default_server: 如果没有设置这个参数,那么将会以在nginx.conf中找到的第一个server块作为默认server块
listen 8080;

#主机名称:其后可以跟多个主机名称,开始处理一个HTTP请求时,nginx会取出header头中的Host,与每个server中的server_name进行匹配,以此决定到底由那一个server来处理这个请求。有可能一个Host与多个server块中的server_name都匹配,这时会根据匹配优先级来选择实际处理的server块。server_name与Host的匹配优先级见文末。
server_name localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

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

##location 语法: location [=||*|^~] /uri/ { … }

location的使用实例见文末。

#注意:location时有顺序的,当一个请求有可能匹配多个location时,实际上这个请求会被第一个location处理。
location / {
proxy_pass http://192.168.1.60;
}

    #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;
#    }
# }
#}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值