安装
sudo apt-get update
sudo apt-get install nginx
systemctl status nginx
参考自how to install and configure nginx on centos 6
安装epel-release
仓库
yum install epel-release
安装nginx
yum -y install nginx
启动nginx服务器
service nginx start
配置系统启时启动nginx
chkconfig nginx on
nginx配置文件为nginx.conf
通常存放在/usr/local/nginx/conf
,/etc/nginx
或者/usr/local/etc/nginx
目录下
nginx
:启动nginx -s stop
: 停止ps -ax | grep nginx
Nginx配置文件{}
组成上下文, 上下文可嵌套, 子上下文可继承父上下文设置.
每个上下文内只能包含它所允许的指令, 包含错误指令nginx会报错.
最外层上下文叫全局上下文或主上下文:
# The main contex is here, outside any other context
...
context {
...
}
主上下文通常用来配置服务器的通用设置, 如worker数量, 保存主进程PID的文件路径.
事件上下文events
包含在主上下文中, 用于全局设置如何处理连接, Nginx配置文件中只能存在一个事件上下文.
events {
# events context
...
}
Nginx使用基于事件的连接处理模型, 所以事件上下文配置决定了worker进程如何处理连接. 这里配置主要用于选择连接处理技术, 或者修改这些方法的实现.
通常连接处理方法会自动选择平台最佳策略. 如Linux系统epoll
通常是最佳选择.
其他配置用于配置一个worker可以处理的连接数, 收到挂起通知时是否挂起所有连接, worker是否轮流响应事件.
HTTP上下文包含处理HTTP和HTTPS连接所需的所有指令和上下文. HTTP上下文和events上下文都是主上下文内的兄弟上下文.
# main context
events {
# events context
}
http {
# http context
}
http上下文的指令定义了其内部包含的所有虚拟服务器的默认值. 常用指令有:
access_log
: 访问日志保存路径error_log
: 错误日志保存路径aio
, sendfile
, directio
: 异步IO文件操作error_page
: 服务器错误时显示页面gzip
, gzip_disable
: gzip压缩配置keepalive_disable
, keepalive_requests
, keepalive_timeout
: TCP链接保持设置sendfile
, tcp_nodelay
, tcp_nopush
: Nginx尝试优化包发送和系统调用root
, index
: 配置应用程序根目录和默认访问文件*_hash_bucket_size
, *_hash_max_size
, server_names
, types
, variables
: ..server上下文声明在http上下文内, 可以有多个声明存在.
# main context
http: {
# http context
server {
# first server context
}
server {
# second server context
}
}
用于区分server的参数:
listen
: 设置服务器监听的ip/端口号server_name
: 服务器所服务的域名location上下文和server有很多共同点, 比如可以定义多个location上下文, 每个上下文处理特定的请求
location match_modified location_match {
}
location上下文需要定义在server上下文内, 允许嵌套这样就可以对特定请求进一步精确处理.
server {
location /match/criteria {}
location /other/criteria {
location nested_match {}
location other_nested {}
}
}
server上下文用ip/端口号以及域名进行区分. location根据请求的URI对发送到所在server的请求进行划分处理.
cp /etc/nginx/nginx.conf /etc/nginx/nignx.conf.backup
也可以使用git进行管理user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
#
开头的是注释http {
# basic settings
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_has_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# logging settings
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
#gzip setting
gzip on;
gzip_disable "misie6";
}
指令介绍:
include
: 被包含的文件内的设置内容被引入到include指令处, 可以包使用通配符包含一批文件: include /etc/nginx/conf.d/*.conf;
gzip
: 启动gzip压缩, 详细内容参考ngx_http_gzip_modulenginx.conf
中http上下文通过include /etc/nginx/sites-enabled/*;
引入配置文件, 这里的文件通常是/etc/nginx/sites-available/
下配置文件的链接文件.
这样可以快速设置虚拟主机的启动配置.
下面是一个例子文件
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name localhost;
location / {
try_files $uri $uri/ /index.html;
}
}
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /usr/local/etc/nginx/myconf/*.conf;
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
# }
#}
include servers/*;
}