您的位置:首页 > 运维架构 > Nginx

【Nginx】nginx初步 从安装开始!

2017-11-28 23:05 260 查看

【Nginx】nginx初步 从安装开始!



前言

Ngine-X——Nginx 是一个开源,自由,高性能web服务器,也是一个反向代理服务器,Nginx是事件驱动的,异步给Nginx带来的是请求高并发,Nginx可以支持C10k,因此,Nginx对比起Apache或者Tomcat都显示出强大的生命力和发展前景。

安装Nginx 现在开始

Nginx的安装需要依赖一些编译工具以及库文件,libtool openssl gcc-c++ zlib

我现在已经安装了这些依赖,可以试着去官网下载合适的nginx安装包。

http://nginx.org/

下载下来后解压



可以看到解压下来的文件夹了。之后cd 进去,找到configure执行,检查安装的依赖的库文件,以及一些配置文件。

但是尽管如此,目前我们还是缺少一个PCRE库文件。它是用来给Nginx提供rewrite功能的库文件。

安装PCRE

下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

同样的进行解压,然后进入文件夹,执行configure,之后进行编译安装

$ ./configure
$ make
$ make install


查看PCRE版本,以验证是否安装成功

pcre-config --version //8.35


安装Nginx

接下来可以很顺利的执行Nginx目录下的configure了。



之后也是同样的操作,编译安装。需要注意的是,在此过程中会在系统目录创建文件夹,因此我们需要root权限

sudo make && make install


安装完成!

执行一下

/usr/local/nginx/sbin/nginx


发现需要root权限

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (13: Permission denied)
2017/11/28 22:54:33 [emerg] 9031#0: open() "/usr/local/nginx/logs/access.log" failed (13: Permission denied)


sudo /usr/local/nginx/sbin/nginx


执行成功!

我们可以打开nginx目录下的conf文件夹进行配置文件的修改

nginx.conf

#user  nobody;
worker_processes  4; //核心数,由于现在的电脑都采用超线程技术,可以将一个物理处理核心模拟成两个逻辑处理核心,因此,假如我是双核,那么我这里写上4

#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;

server {
listen       80; //监听端口
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;
#    }
#}

}


进行响应的配置过后,就可以开始打开浏览器进行访问,由于默认端口是80,直接访问localhost



服务器已经可以访问!

总结

Nginx可以利用反向代理实现负载均衡,可以配合Node使用,Node虽然也有cluster,可以开启多个进程进行负载均衡,但node对文件的处理不如Nginx好,可以在客户端与node之间架入Nginx来实现请求分发负载均衡,提高用户体验和服务器性能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: