Nginx安装

进入/usr/local/src目录下

[root@100xuni1 ~]# cd /usr/local/src/         

下载Nginx安装包可以去nginx.org或者https://coding.net/u/aminglinux/p/resource/git/blob/master/README.md下载

[root@100xuni1 src]# wget http://124.205.69.170/files/51490000069A64B9/nginx.org/download/nginx-1.14.0.tar.gz                    ##wget下载

解压

[root@100xuni1 src]# tar zxf nginx-1.14.0.tar.gz      ##解压
[root@100xuni1 src]# cd nginx-1.14.0/                    ##解压完成后进入

编译

[root@100xuni1 nginx-1.14.0]#  ./configure --prefix=/usr/local/nginx       ##编译

make

[root@100xuni1 nginx-1.14.0]# make

** make install**

[root@100xuni1 nginx-1.14.0]# make install

nginx目录

[root@100xuni1 nginx-1.14.0]# ls /usr/local/nginx/       ##目录
conf  html  logs  sbin
[root@100xuni1 nginx-1.14.0]# ls /usr/local/nginx/conf/     ##配置文件目录
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@100xuni1 nginx-1.14.0]# ls /usr/local/nginx/html/       ##样例文件
50x.html  index.html
[root@100xuni1 nginx-1.14.0]# ls /usr/local/nginx/logs/            ##存放日志
[root@100xuni1 nginx-1.14.0]# ls /usr/local/nginx/sbin/      ##他的进程核心的文件
nginx

-t也是支持的查询配置文件是否有错

[root@100xuni1 nginx-1.14.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

给nginx创建配置文件还要给他做个启动脚本

[root@100xuni1 nginx-1.14.0]# vim /etc/init.d/nginx       ##启动脚本存放位置,配置这个文件内容去https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx 把这里的内容拷贝到nginx里

改权限

[root@100xuni1 nginx-1.14.0]#  chmod 755 /etc/init.d/nginx

开机启动

[root@100xuni1 nginx-1.14.0]#  chkconfig --add nginx 
[root@100xuni1 nginx-1.14.0]# chkconfig nginx on 

编辑配置文件这个配置文件需要下载模板

[root@100xuni1 nginx-1.14.0]# cd /usr/local/nginx/conf/    ##进入配置文件
[root@100xuni1 conf]# ls          ##这个里边nginx.conf了不用他的用自己的,
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@100xuni1 conf]# mv nginx.conf nginx.conf.1   ##拷贝改名字

开始配置

[root@100xuni1 conf]# vim nginx.conf     ##去https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf下拷贝内容

user nobody nobody;                ##用来启动nginx是哪些用户
worker_processes 2;               ##定义子进程有几个
error_log /usr/local/nginx/logs/nginx_error.log crit;      ##错误日志
pid /usr/local/nginx/logs/nginx.pid;             ##pid
worker_rlimit_nofile 51200;       ##nginx最多打开多少个文件

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;

    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;

        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}
[root@100xuni1 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动

[root@100xuni1 conf]# /etc/init.d/nginx  start

测试

[root@100xuni1 conf]# curl localhost

nginx也支持php解析

默认虚拟主机

配置虚拟主机之前做个操作编辑一下nginx.conf的配置

[root@100xuni1 vhost]# vim aaa.com.conf

server
{
    listen 80 default_server;  
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}
[root@100xuni1 vhost]# ls
aaa.com.conf
[root@100xuni1 vhost]# cat aaa.com.conf 
server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

创建一个目录

[root@100xuni1 vhost]# mkdir /data/wwwroot/default
[root@100xuni1 vhost]# cd /data/wwwroot/default/       ##进入default目录下
[root@100xuni1 default]#vim index.html           ##定义一段This is the default site.

以上操作完检查有没有错误

[root@100xuni1 default]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重启或者重新加载

[root@100xuni1 default]# /etc/init.d/nginx restart      ##重启
[root@100xuni1 default]# /usr/local/nginx/sbin/nginx -s reload     ##重新加载

做下测试

[root@100xuni1 default]# curl localhost
This is the default site.            ##刚刚定义的index.html
[root@100xuni1 default]# ls
index.html

Nginx用户认证

创建一个虚拟主机

[root@100xuni1 vhost]# vim test.com.conf     #在vhost目录下创建
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
    location  /
    {                 ##一下都是用户认证相关的配置
        auth_basic              "Auth";     ##定义用户认证的名字             
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;   ##用户名密码文件
    }
}

怎样生成这个用户名密码文件。它也用到了apache生成用户密码的工具,如果以前用过apache直接/usr/local/apache2.4/bin/htpasswd,如果没有安装直接用yum install -y httpd,以前做过实验就不去安装

[root@100xuni1 vhost]# /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd hanshuo
New password: 
Re-type new password: 
Adding password for user hanshuo
[root@100xuni1 vhost]# cat /usr/local/nginx/conf/htpasswd
hanshuo:$apr1$oe8T3EAL$Lafh6D4c/2p4GphCu2TNP1       ##生成的用户密码 如果想在创建用户密码就不用加-C了,如果加-C就是重置
[root@100xuni1 vhost]# /usr/local/apache2.4/bin/htpasswd /usr/local/nginx/conf/htpasswd user1
New password: 
Re-type new password: 
Adding password for user user1
[root@100xuni1 vhost]# cat /usr/local/nginx/conf/htpasswd
hanshuo:$apr1$oe8T3EAL$Lafh6D4c/2p4GphCu2TNP1
user1:$apr1$KDxmO2Ye$unsM3hxh5X2QR/JGqSjH40



测试配置并重新加载

[root@100xuni1 vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@100xuni1 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@100xuni1 vhost]# mkdir /data/wwwroot/test.com      ##创建主目录
[root@100xuni1 vhost]# echo “test.com”>/data/wwwroot/test.com/index.html   ##创建一个index.html

测试

[root@100xuni1 vhost]# curl -x127.0.0.1:80 test.com  ##显示的是401需要用户验证
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center>401 Authorization Required</center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

[root@100xuni1 vhost]# curl -uhanshuo:hanshuo -x127.0.0.1:80 test.com 

“test.com”

##如果想用admin去访问

[root@100xuni1 vhost]# vim test.com.conf

server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

    location  /admin/    ##把这里加个admin就可以了
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;


[root@100xuni1 vhost]# curl -x127.0.0.1:80 test.com
“test.com”
[root@100xuni1 vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center>401 Authorization Required</center>
<hr><center>nginx/1.14.0</center>
</body>
</html>
[root@100xuni1 vhost]# mkdir /data/wwwroot/test.com/admin
[root@100xuni1 vhost]# echo "test.com.conf dir" > /data/wwwroot/test.com/admin/index.html 
[root@100xuni1 vhost]# curl -uhanshuo:hanshuo -x127.0.0.1:80 test.com/admin/
test.com.conf dir        ##这是针对目录的 

Nginx域名重定向

改下配置文件

[root@100xuni1 vhost]# vim test.com.conf

server
{
    listen 80;
    server_name test.com test2.com test3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }   
}   

扩展 Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html

apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.html

mod_php 和 mod_fastcgi以及php-fpm的比较 http://dwz.cn/1lwMSd

概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM http://www.nowamagic.net/librarys/veda/detail/1319/

https://www.awaimai.com/371.html