Nginx基本入门
2017-04-04 07:36
211 查看
本文转载至:http://blog.csdn.net/u012486840/article/details/53098890
首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。
配置:
windows版本:
server {
listen 80; //监听端口
server_name localhost; //http 服务器名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root B:\\nginx-1.8.1\\html; //http 服务器 项目路径位置(绝对路径)
index index.html; //http 服务器 默认首页
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //http 服务器 错误页面重定向功能
location = /50x.html {
root html;
}
Linux 版本
server {
listen 80; # 端口号
location / {
root /usr/share/nginx/html; # 静态文件路径
}
}
什么是反向代理?
客户端本来可以直接通过HTTP协议访问某网站应用服务器,如果网站管理员在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向代理服务器。
反向代理
配置:
windows 版本
server {
listen 8081;
server_name 192.168.0.104;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.0.104:8080; }
#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;
}
当网站访问量非常大,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。于是将相同的应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其他服务器正常运行,就不会影响用户使用。
Nginx可以通过反向代理来实现负载均衡。
负载均衡
配置:
upstream netitcast.com {
server 192.168.0.104:8080; # 应用服务器1
server 192.168.0.104:8090; # 应用服务器2
}
server {
listen 80;
server_name 192.168.0.104;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://netitcast.com; proxy_redirect default;
}
#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;
#}
}
的网站访问量大,需要负载均衡。然而并不是所有网站都如此出色,有的网站,由于访问量太小,需要节省成本,将多个网站部署在同一台服务器上。
例如将www.aaa.com和www.bbb.com两个网站部署在同一台服务器上,两个域名解析到同一个IP地址,但是用户通过两个域名却可以打开两个完全不同的网站,互相不影响,就像访问两个服务器一样,所以叫两个虚拟主机。
配置:
在服务器8080和8081分别开了一个应用,客户端通过不同的域名访问,根据server_name可以反向代理到对应的应用服务器。
虚拟主机的原理是通过HTTP请求头中的Host是否匹配server_name来实现的,有兴趣的同学可以研究一下HTTP协议。
另外,server_name配置还可以过滤有人恶意将某些域名指向你的主机服务器。
5、FastCGI
Nginx本身不支持PHP等语言,但是它可以通过FastCGI来将请求扔给某些语言或框架处理(例如PHP、Python、Perl)。
server {
listen 80;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /PHP文件路径$fastcgi_script_name; # PHP文件路径
fastcgi_pass 127.0.0.1:9000; # PHP-FPM地址和端口号
# 另一种方式:fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
配置中将.php结尾的请求通过FashCGI交给PHP-FPM处理,PHP-FPM是PHP的一个FastCGI管理器。有关FashCGI可以查阅其他资料,本文不再介绍。
fastcgi_pass和proxy_pass有什么区别?下面一张图带你看明白:
1、静态HTTP服务器
首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。配置:
windows版本:
server {
listen 80; //监听端口
server_name localhost; //http 服务器名称
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root B:\\nginx-1.8.1\\html; //http 服务器 项目路径位置(绝对路径)
index index.html; //http 服务器 默认首页
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; //http 服务器 错误页面重定向功能
location = /50x.html {
root html;
}
Linux 版本
server {
listen 80; # 端口号
location / {
root /usr/share/nginx/html; # 静态文件路径
}
}
2、反向代理服务器
什么是反向代理?客户端本来可以直接通过HTTP协议访问某网站应用服务器,如果网站管理员在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向代理服务器。
反向代理
配置:
windows 版本
server {
listen 8081;
server_name 192.168.0.104;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.0.104:8080; }
#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;
}
3、负载均衡
当网站访问量非常大,也摊上事儿了。因为网站越来越慢,一台服务器已经不够用了。于是将相同的应用部署在多台服务器上,将大量用户的请求分配给多台机器处理。同时带来的好处是,其中一台服务器万一挂了,只要还有其他服务器正常运行,就不会影响用户使用。Nginx可以通过反向代理来实现负载均衡。
负载均衡
配置:
upstream netitcast.com {
server 192.168.0.104:8080; # 应用服务器1
server 192.168.0.104:8090; # 应用服务器2
}
server {
listen 80;
server_name 192.168.0.104;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://netitcast.com; proxy_redirect default;
}
#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;
#}
}
4、虚拟主机
的网站访问量大,需要负载均衡。然而并不是所有网站都如此出色,有的网站,由于访问量太小,需要节省成本,将多个网站部署在同一台服务器上。例如将www.aaa.com和www.bbb.com两个网站部署在同一台服务器上,两个域名解析到同一个IP地址,但是用户通过两个域名却可以打开两个完全不同的网站,互相不影响,就像访问两个服务器一样,所以叫两个虚拟主机。
配置:
server { listen 80 default_server; server_name _; return 444; # 过滤其他域名的请求,返回444状态码 } server { listen 80; server_name www.aaa.com; # www.aaa.com域名 location / { proxy_pass http://localhost:8080; # 对应端口号8080 } } server { listen 80; server_name www.bbb.com; # www.bbb.com域名 location / { proxy_pass http://localhost:8081; # 对应端口号8081 } }
在服务器8080和8081分别开了一个应用,客户端通过不同的域名访问,根据server_name可以反向代理到对应的应用服务器。
虚拟主机的原理是通过HTTP请求头中的Host是否匹配server_name来实现的,有兴趣的同学可以研究一下HTTP协议。
另外,server_name配置还可以过滤有人恶意将某些域名指向你的主机服务器。
5、FastCGI
Nginx本身不支持PHP等语言,但是它可以通过FastCGI来将请求扔给某些语言或框架处理(例如PHP、Python、Perl)。
server {
listen 80;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /PHP文件路径$fastcgi_script_name; # PHP文件路径
fastcgi_pass 127.0.0.1:9000; # PHP-FPM地址和端口号
# 另一种方式:fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
配置中将.php结尾的请求通过FashCGI交给PHP-FPM处理,PHP-FPM是PHP的一个FastCGI管理器。有关FashCGI可以查阅其他资料,本文不再介绍。
fastcgi_pass和proxy_pass有什么区别?下面一张图带你看明白:
相关文章推荐
- Nginx入门级简介,包括安装,基本使用,负载均衡,动静分离,反向代理,缓存应用等功能。
- Nginx基础入门之nginx基本介绍(1)
- Nginx基本入门
- Nginx基本功能极速入门
- Nginx基本功能极速入门
- Nginx模块开发---Nginx开发从入门到精通 读书笔记(三)---Nginx基本数据结构之 ngx_pool_t
- Nginx基本入门
- Nginx的一些基本功能极速入门
- Nginx基本功能极速入门
- nginx 基本入门教程
- Nginx基础入门之nginx基本介绍(2)
- Nginx的一些基本功能极速入门
- Nginx的一些基本功能极速入门
- Nginx基本功能极速入门
- Nginx的一些基本功能极速入门
- Nginx的一些基本功能极速入门
- Nginx的一些基本功能极速入门
- Nginx基本功能极速入门
- Nginx 基本入门
- Nginx基本入门