阿里云ECS每天一件事D6:安装nginx-1.6.2

自从接触nginx就开始喜欢上这个小东西了,似乎没什么特别的原因,就是喜欢而已。

1、安装环境的准备

yum install pcre pcre-devel openssl openssl-devel

由于前面的安装,大多数环境和类库已经准备完毕,只需要安装rewrite依赖和ssl相关的组件即可。

2、编译配置

./configure --prefix=/usr/local/nginx --user=www-data --group=www-data
 --with-http_ssl_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module
 --with-mail

常规性编译配置,大多数标准组件,nginx编译时会默认编译

3、安装

make && make install

4、启动操作

1 /usr/local/nginx/sbin/nginx
2 /usr/local/nginx/sbin/nginx –s stop|reload

nginx的操作基本都是通过nginx这个命令进行的,启动可直接运行1,停止,或者修改完配置文件重新加载,可以运行带有-s参数的命令执行

5、配置

nginx的配置文件位于/usr/local/nginx/conf/nginx.conf。

打开后,注意如下的基础配置语句:

 1 user www-data www-data;
 2 worker_processes 2;
 3 
 4 events {
 5     worker_connections 2048;
 6 }
 7 
 8 http {
 9     include mime.types;
10     include vhost/*.conf;
11     default_type application/octet-stream;
12     index index.php index.html;
13 }

1)运行nginx的用户和组

2)nginx可使用的cpu内核数,默认是1,worker_processes*worker_connections=实际可接受的用户链接数字;

5)设置可接受的连接数;

10)自定义配置,标示关于虚拟主机的配置文件,在conf目录的vhost子目录中(注意此为自定义配置,只有在http全局设置中使用了include加载全部虚拟主机配置方可有效)。

虚拟主机配置:

 1 server {
 2     listen      80;
 3     server_name example.org www.example.org;
 4     root        /data/www;
 5     access_log    logs/example/access.log;
 6     
 7     location ~ .php$ {
 8         fastcgi_index index.php;
 9         fastcgi_pass 127.0.0.1:9000;
10         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
11         include fastcgi_params;
12     }
13 
14     location ~* .(gif|jpg|png)$ {
15     access_log off;
16         expires 5d;
17     }
18 
19     location ~ /.ht {
20         deny all;
21     }
22 }

具体配置内容,可参考wiki.nginx.org

6、默认主机的设置

配置完上面的虚拟主机,有时你会发现一个有意思的事情,如果你绑定一个未定义的主机头,依旧是可以访问的,只不过访问的是,第一个或者最后一个虚拟主机,这是因为nginx默认没有找到主机头时,会指定一个,因此最好在主配置文件中,设置默认的server配置节,已避免非授权的绑定。

server {
    listen 80 default_server;
    server_name localhost;
    location / {
        root html;
    }
}

注意,上面的关键词为default_server,设置一个空的虚拟主机,将其监听设置为default_server,这样一来,所有为在虚拟主机中设定的主机头,及时绑定之后,也会默认跳转到这个空主机中。

原文地址:https://www.cnblogs.com/bashenandi/p/4037130.html