一、nginx安装
1.安装linux系统下的一些辅助工具
`yum -y install gcc gcc-c++ autoconf pcre-devel make automake wget httpd-tools `
复制代码
2.使用 cd / 切换到root目录下,建立一个自己的用户目录
`mkdir klierbyck`
`cd klierbyck`
`mkdir app backup soft logs works`
复制代码
3.查看nginx源的版本
`yum list | grep nginx`
复制代码
4.打开http://nginx.org/en/download.html查看最新的Stable version
5.打开http://nginx.org/en/linux_packages.html找到如下代码
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
复制代码
6.建立nginx源的配置文件
`vim /etc/yum.repos.d/nginx.repo`
复制代码
7.蒋如下代码修改后放到nginx.repo中并保存退出
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
复制代码
8.再次查看是否有最新稳定版本
`yum list | grep nginx`
复制代码
9.安装nginx
`yum install nginx`
复制代码
10.查看nginx是否安装成功
`nginx -v`
复制代码
二、nginx配置说明
1.查看nginx安装到哪里
`rpm -ql nginx`
复制代码
2.查看nginx.conf
`cd /etc/nginx`
`vim nginx.conf`
复制代码
#配置说明
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
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; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
}
复制代码
3.查看default.conf
`cd /etc/nginx/conf.d`
`vim default.conf`
复制代码
#配置说明
server {
listen 80; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/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;
#}
}
复制代码
4.进入网站程序存放目录
`cd /usr/share/nginx/html`
复制代码
5.阿里云的安全组配置
进入阿里云控制台,并找到ECS实例。
点击实例后边的“更多”
点击“网络和安全组” ,再点击“安全组配置”
右上角添加“安全组配置”
进行80端口的设置,具体设置如图就好。
复制代码
6.启动nginx
`nginx`
复制代码
7.使用服务器公网IP访问网站
三、nginx常用命令
1.nginx服务运行情况查询
`ps aux | grep nginx`
复制代码
2.启动
`nginx`
`systemctl start nginx.service`
复制代码
3.停止
`nginx -s stop`(立即停止服务)
`nginx -s quit`(从容停止服务)
`killall nginx`(杀死进程)
`systemctl stop nginx.service`(systemctl 停止)
复制代码
4.重启
`systemctl restart nginx.service`
复制代码
5.重新载入
`nginx -s reload`
复制代码
6.查看开启的端口号
`netstat -tln`
复制代码
四、错误页面和访问设置
vim /etc/nginx/conf.d/default.conf
#错误页面设置
error_page 404 /404.html;
error_page 404 http://www.klierbyck.com;
error_page 500 502 503 504 /50x.html;
#访问限制
location / {
deny 123.9.51.42; //拒绝访问的ip
allow 45.76.202.231; //可以访问的ip
}
location / {
allow 45.76.202.231;
}
#说明:在同一个块下的两个权限指令,先出现的设置会覆盖后出现的设置
#对于网站下的img(图片目录)是运行所有用户访问,但对于网站下的admin目录则只允许公司内部固定IP访问。
location =/img{
allow all;
}
location =/admin{
deny all;
}
#说明:=号代表精确匹配,使用了=后是根据其后的模式进行精确匹配。
#使用正则表达式匹配
deny all;
}
#这样我们就不能访问php结尾的文件了
复制代码
五、Nginx设置虚拟主机
1.基于端口号设置的虚拟主机
在etc/nginx/conf.d/下建立一个8001.conf文件,将如下代码复制进去并保存
server{
listen 8001;
server_name localhost;
location / {
root /usr/share/nginx/html/html8001;
index index.html index.htm;
}
}
复制代码
在/usr/share/nginx/html/下面建立html8001文件夹并放置一个index.html
重载nginx后通过IP:8001即可访问网站
2.基于IP设置的虚拟主机
在etc/nginx/conf.d/下建立一个ip1.conf文件,将如下代码复制进去并保存
server{
listen 80;
server_name IP地址;
location / {
root /usr/share/nginx/html/ip1;
index index.html index.htm;
}
}
复制代码
在/usr/share/nginx/html/下面建立ip1文件夹并放置一个index.html
重载nginx后通过IP地址即可访问网站
3.基于域名设置的虚拟主机
将域名解析到服务器公网ip,在etc/nginx/conf.d/下建立一个test.conf文件,将如下代码复制进去并保存
server{
listen 80;
server_name test.klierbyck.com;
location / {
root /usr/share/nginx/html/test;
index index.html index.htm;
}
}
复制代码
在/usr/share/nginx/html/下面建立test文件夹并放置一个index.html
重载nginx后通过test.klierbyck.com即可访问网站
六、Nginx反向代理的设置
将域名解析到服务器公网ip,在etc/nginx/conf.d/下建立一个proxy.conf文件,将如下代码复制进去并保存
server{
listen 80;
server_name proxy.klierbyck.com;
location / {
proxy_pass http://www.klierbyck.com;
}
}
复制代码
重载nginx后通过proxy.klierbyck.com即可访问网站
反向代理一些常用的指令:
proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。
proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。
proxy_read_timeout : 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。
proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。
proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh。
复制代码
七、Nginx适配PC或移动设备
建立pc端和移动端程序存放目录
cd /usr/share/nginx
mkdir pc
mkdir mobile
复制代码
将下面匹配内容写入etc/nginx/conf.d/default.conf
server{
listen 80;
server_name www.klierbyck.com;
location / {
root /usr/share/nginx/pc;
if ($http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL\-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ) {
root /usr/share/nginx/mobile;
}
index index.html;
}
}
复制代码
八、开启gzip压缩
运行一下命令编辑nginx.conf
vim etc/nginx/nginx.conf
将http对象中加入如下配置项
http {
gzip on;
gzip_types text/plain application/javascript text/css;
}
复制代码
nginx更多配置
gzip : 该指令用于开启或 关闭gzip模块。
gzip_buffers : 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。
gzip_comp_level : gzip压缩比,压缩级别是1-9,1的压缩级别最低,9的压缩级别最高。压缩级别越高压缩率越大,压缩时间越长。
gzip_disable : 可以通过该指令对一些特定的User-Agent不使用压缩功能。
gzip_min_length:设置允许压缩的页面最小字节数,页面字节数从相应消息头的Content-length中进行获取。
gzip_http_version:识别HTTP协议版本,其值可以是1.1.或1.0.
gzip_proxied : 用于设置启用或禁用从代理服务器上收到相应内容gzip压缩。
gzip_vary : 用于在响应消息头中添加Vary:Accept-Encoding,使代理服务器根据请求头中的Accept-Encoding识别是否启用gzip压缩。
复制代码