nginx

来源:互联网 发布:字有几个字节知乎 编辑:程序博客网 时间:2024/06/07 07:26
Nginx 简介
Nginx 是一个轻量级高性能的 web 服务器,它是为快速响应大量静态文件请求和高效利用系统资源而设
计的。与 apache 使用面向进程或线程的方式处理请求不同, nginx 使用异步事件驱动模型在负载下性能
更突出。
虽然 nginx 能高效地服务静态文件,但也有人认为 nginx 处理动态内容并不理想。不像 apache 服务器,
nginx 没用使用内嵌解释器的方式 来处理动态内容。相反,动态内容被丢给 cgi , fastcgi 或者像 apache
这样的 web 服务器,然后把处理结果返回给 nginx , nginx 在返给 浏览器。这种方式就导致部署起来会
更复杂一些。出于这些原因,使用和配置 nginx 可能会晦涩。 nginx 的配置感觉更复杂或者不直接。

Nginx 安装 :自己百度下载安装包及组件
wget wget http://nginx.org/download/nginx-1.12.0.tar.gzwget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gzwget http://zlib.net/zlib-1.2.11.tar.gzwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gzyum install gcc-c++安装 Nginx 及相关组件openssl 安装 :[root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz省略安装内容...[root@localhost src]# cd openssl-fips-2.0.10[root@localhost openssl-fips-2.0.10]# ./config && make && make install省略安装内容 ...pcre 安装:[root@localhost src]# tar zxvf pcre-8.40.tar.gz省略安装内容...[root@localhost src]# cd pcre-8.40[root@localhost pcre-8.40]# ./configure && make省略安装内容...zlib 安装:[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz省略安装内容 ...[root@localhost src]# cd zlib-1.2.11[root@localhost zlib-1.2.11]# ./configure && make && make install省略安装内容...nginx 安装:[root@localhost src]# tar zxvf nginx-1.12.0.tar.gz省略安装内容...[root@localhost src]# cd nginx-1.12.0[root@localhost nginx-1.12.0]# ./configure && make && make install省略安装内容...


启动 Nginx

先找一下 nginx 安装到什么位置上了 :


进入 nginx 目录并启动:


报错了, error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such
file or directory ,按照下面方式解决

[root@server1 nginx]# whereis libpcre.so.1libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so.1 /usr/local/lib/libpcre.so[root@server1 nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64[root@server1 nginx]# sbin/nginx[root@server1 nginx]# ps -aux | grep nginxWarning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQroot 11039 0.0 0.1 20404 648 ?Ss 19:08 0:00 nginx: master process sbin/nginxnobody 11040 0.0 0.2 20848 1240 ?S 19:08 0:00 nginx: worker processroot 11042 0.0 0.1 103252 868 pts/0 S+ 19:08 0:00 grep nginx
进入 Linux 系统的图形界面,打开浏览器输入 localhost 会看到下图,说明 nginx 启动成功



nginx 的基本操作
启动
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重启

[root@localhost~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令帮助

[root@localhost~]# /usr/local/nginx/sbin/nginx -h

验证配置文件

[root@localhost~]# /usr/local/nginx/sbin/nginx -t

配置文件
[root@localhost~]# vim /usr/local/nginx/conf/nginx.conf

配置 :


[root@server1 nginx]# cd /usr/local/nginx/[root@server1 nginx]# lsclient_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp[root@server1 nginx]# cd conf/[root@server1 conf]# lsfastcgi.confkoi-utfnginx.confuwsgi_paramsfastcgi.conf.default koi-winnginx.conf.default uwsgi_params.defaultfastcgi_paramsmime.typesscgi_paramswin-utffastcgi_params.default mime.types.default scgi_params.default[root@server1 conf]# vim nginx.conf

#user nobody;wnginxorker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pidlogs/nginx.pid;events {worker_connections 1024;}
这些是配置文件开始的默认行。通常的环境下,你不需要修改这些选项。这一部分有几个方面需要我们
注意:
• 所有以 # 号开的行是注释, nginx 不会解析。默认的配置文件有许多说明解释的注释块
• 指令是以一个变量名开头 ( 例如, worker_processes 或 pid), 然后包含一个参数 ( 例如, 1 或
logs/nginx.pid) 或者多个参数 ( 例如, "logs/error.log notice")
• 所有指令以分号结尾
• 某些指令,像上面的 events 可以包含多个子指令作为参数。这些子指令以花括号包围。
• 虽然 nginx 不解析空白符 ( 例如 tab ,空格,和换行符 ) ,但是良好的缩进能提高你维护长期运行配
置文件的效率。良好的缩进使配置文件读起来更流畅,能让你很容易明白配置的策略,即使几个
月前。

http {includemime.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;sendfileon;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;

"http { }" 块的开头像配置文件的开头一样都是标准配置不需要修改。这里我们需要把注意力放在这些元
素上 :
• 这部分内容的开始 "include" 语句包含 /usr/loca/nginx/mime.types 文件到 nginx.conf 文件
include 语句所在位置。 include 对 ningx.conf 文件的可读性和组织性很有用。
• 不能过多使用 include ,如果太多递归地 include 文件会产生混乱,所以需要合理有限制地使用
include 来保证配置文件的清晰和可管理。• 你可以去掉 log_format 指令前的注释并修改这几行设置的变量为你想记录的信息。
• gzip 指令告诉 nginx 使用 gzip 压缩的方式来降低带宽使用和加快传输速度。如果想使用 gzip 压
缩,需要添加如下配置到配置文件的 gzip 位置。

虚拟主机 server 配置

Server {                 ## 虚拟主机 1listen   80;server_name nginx.westos.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root /westos;index index.html index.htm;}}server {       ## 虚拟主机 2listen   80;server_name haha.westos.com;#charset koi8-r;#access_log logs/host.access.log main;location / {root /haha;index index.html index.htm;}}
测试:




反向代理,负载均衡

upstream proxy_test {      ##upstream 就是配置负载均衡的,当然得两台以上才叫负载server 172.25.254.2:80 weight=1;server 172.25.254.3:80 weight=1;#ip_hash;                 ## 当负载两台以上用 ip 来 hash 解决 session 的问题,一台就别 hash 了。}server {listen  80;server_name www.westos.com; # 要访问的域名,我这里用的测试域名,如果有多个,用逗号分开location / {proxy_passhttp://proxy_test;        # 与 upstream 后面的名字对应    proxy_set_header Host   $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

测试:输入域名后可以访问这个页面


手动刷新以下他就会跳到下面这个页面

























原创粉丝点击