nginx在linux下安装,nginx在linux下的安装与使用

如已安装过,此处的路径为安装目录;若未安装,则此路径为编译安装包路径,nginx将执行模块的默认编译安装。

启动nginx之后,浏览器中输入http://localhost可以验证是否安装启动成功,成功页面如下:

0818b9ca8b590ca3270a3433284dd417.png

2.Nginx配置

安装完成之后,配置目录conf下有以下配置文件,过滤掉了xx.default配置:

smart@ubuntu:/opt/nginx-1.7.7/conf$ tree |grep -v default

├── fastcgi.conf

├── fastcgi_params

├── koi-utf

├── koi-win

├── mime.types

├── nginx.conf

├── scgi_params

├── uwsgi_params

└── win-utf

除了nginx.conf,其余配置文件,一般只需要使用默认提供即可。

1.nginx.conf

nginx.conf是主配置文件,默认配置去掉注释之后的内容如下图所示:

worker_process表示工作进程的数量,一般设置为cpu的核数

worker_connections表示每个工作进程的最大连接数

server{}块定义了虚拟主机

listener监听端口

server_name监听域名

location{}是用来为匹配的 URI 进行配置,URI 即语法中的“/uri/”。location /{}匹配任何查询,因为所有请求都以/开头

root指定对应uri的资源查找路径,这里html为相对路径,完整路径为/opt/ opt/nginx-1.7.7/html/

index指定首页index文件的名称,可以配置多个,以空格分开。如有多个,按配置顺序查找

worker_processes 1;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

sendfileon;

keepalive_timeout65;

server {

listen80;

server_namelocalhost;

location / {

roothtml;

indexindex.html index.htm;

}

# redirect server error pages to the static page /50x.html

error_page500 502 503 504 /50x.html;

location = /50x.html {

roothtml;

}

}

}

从配置可以看出,nginx监听了80端口、域名为localhost、跟路径为html文件夹(我的安装路径为/opt/nginx-1.7.7,所以/opt/nginx-1.7.7/html)、默认index文件为index.html, index.htm、服务器错误重定向到50x.html页面。

可以看到/opt/nginx-1.7.7/html/有以下文件:

smart@ubuntu:/opt/nginx-1.7.7/html$ ls

50x.html index.html

这也是上面在浏览器中输入http://localhost,能够显示欢迎页面的原因。实际上访问的是/opt/nginx-1.7.7/html/index.html文件。

2.mime.types

文件扩展名与文件类型映射表,nginx根据映射关系,设置http请求响应头的Content-Type值。当在映射表找不到时,使用nginx.conf中default-type指定的默认值。例如,默认配置中的指定的default-type为application/octet-stream。

include mime.types;

default_type application/octet-stream;

下面截一段mime.types定义的文件扩展名与文件类型映射关系,完整的请自行查看:

types {

text/htmlhtml htm shtml;

text/csscss;

text/xmlxml;

image/gifgif;

image/jpegjpeg jpg;

application/javascriptjs;

application/atom+xmlatom;

application/rss+xmlrss;

text/mathmlmml;

text/plaintxt;

text/vnd.sun.j2me.app-descriptorjad;

text/vnd.wap.wmlwml;

text/x-componenthtc;

...

}

3.fastcgi_params

nginx配置Fastcgi解析时会调用fastcgi_params配置文件来传递服务器变量,这样CGI中可以获取到这些变量的值。默认传递以下变量:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #脚本文件请求的路径

fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123

fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)

fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段

fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。

fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称

fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数

fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。

fastcgi_param DOCUMENT_ROOT $document_root;#网站的根目录。在server配置中root指令中指定的值

fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

fastcgi_param HTTPS $https if_not_empty;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;#cgi 版本

fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;#nginx 版本号,可修改、隐藏

fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP

fastcgi_param REMOTE_PORT $remote_port; #客户端端口

fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址

fastcgi_param SERVER_PORT $server_port; #服务器端口

fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name

#fastcgi_param PATH_INFO $path_info;#可自定义变量

# PHP only, required if PHP was built with --enable-force-cgi-redirect

#fastcgi_param REDIRECT_STATUS 200;

这些变量的作用从其命名也可以看出。

4.fastcgi.conf

对比下fastcgi.conf与fastcgi_params文件,可以看出只有以下差异:

smart@ubuntu:/opt/nginx-1.7.7/conf$ diff fastcgi.conf fastcgi_params

2d1

< fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

即fastcgi.conf只比fastcgi_params多了一行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值