您的位置:首页 > 运维架构 > Nginx

1、Nginx安装和配置文件

2016-04-29 18:33 543 查看
这两天一直在看Nginx相关视频资料,但是由于知识点是在太多,所以在看过视频之后总是记不住,做过的实验第二天也忘了怎么做,所以决定将学习笔记写到博客里面加深映像,学习本来就是一个举一反三的过程,温故知新嘛。

一、Nginx的安装
Nginx的安装很简单,有两种安装方式:
1、源码编译安装。
去官网下载源码包,地址:http://nginx.org/ ,生产环境最好是下载stabe版本。要稳定一些。安装过程不再作说明,网上能搜到一大堆。
2、RPM包安装
官网下载stabe版本的RPM包安装,地址:http://nginx.org/packages/centos/6/x86_64/RPMS/
或者创建一个repo文件,文件内容如下:然后用yum安装,过程如下:
#vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
注意baseurl中6可以用数字替换,centos也可以用rhel代替,根据自己系统的版本来设置URL地址。
下面是官网链接:http://nginx.org/en/linux_packages.html#stable

二、Nginx的配置文件
我是下载RPM包安装的。生成配置文件路径如下:
/etc/nginx/nginx.conf
这个文件是nginx的全局配置文件,包含了全局配置,event段相关设定,http段相关设定。

注意,event段和http段有花括号引起来。文件结构为:
event {
...
}
http {
...
}
文件内容如下:
###全局配置
user  nginx;      ###指定nginx启动用户
worker_processes  4;  ###启动nginx服务时,启动的worker process数 可以设置为auto,但一般设置为与CPU核心数相等,比如,CPU为4核,则设置为4,或者设置为CPU核心数减去1。
error_log  /var/log/nginx/error.log warn;   ###错误日志的路径
pid        /var/run/nginx.pid;  ###pid的路径
###event段设置
events {
worker_connections  1024;   ###每个进程的最大连接数,与worker_processes数相乘为nginx服务器的最大连接数
}
http {
include       /etc/nginx/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  /var/log/nginx/access.log  main;
sendfile        on;
#tcp_nopush     on;
keepalive_timeout  65;
#gzip  on;
include /etc/nginx/conf.d/*.conf;  ###包含了/etc/nginx/conf.d目录下所有以".conf"结尾的文件,注意这条很重要,因为在默认配置中没有server段的配置,而一般都是将server的配置放在
/etc/nginx/conf.d/目录下。
}
正常情况下http段配置结构如下:
http {
...
server {
...
location [PATTERN]  {
...
}
location [PATTERN]  {
...
}
}

server {
...
}
}
每一个http段中可以包含多个server段,每一个server段可以包含多个location段。不同的server配置最好是用单独的文件存放,都放在/etc/nginx/conf.d/目录下,只要以".conf"结尾即可

三、Nginx相关命令和服务启动停止
运行nginx -h查看nginx的相关命令参数

root@localhost:~# nginx -h
nginx version: nginx/1.8.1
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h         : this help
-v            : show version and exit
-V            : show version and configure options then exit
-t            : test configuration and exit
-q            : suppress non-error messages during configuration testing
-s signal     : send signal to a master process: stop, quit, reopen, reload
-p prefix     : set prefix path (default: /etc/nginx/)
-c filename   : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
用的较多就是
nginx -s reload :重新加载nginx的配置文件;
nginx -t:检查配置文件语法是否有问题;

Nginx服务启动、停止、重新加载
#service nginx start | stop | reload
#/etc/init.d/nginx start | stop | reload

本文出自 “zengestudy” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: