Nginx 安装
首先进入 / usr/local/src 目录. 然后下载 Nginx.
wget http://nginx.org/download/nginx-1.12.1.tar.gz
然后解压
tar zxf nginx-1.12.1.tar.gz
然后进入我们刚才解压好的目录进行编译
- cd nginx-1.12.1
- [root@linletao-001 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
这里没有加编译参数, 我们可以根据实际情况, 在后期编译的时候去添加参数.
在以后我们安装环境的时候, 源码包一定要保留, 因为我们不一定需要什么模块, 所以保留源码包还是很必要的.
编译完成后我们进行安装
make && make install
安装完后我们会得到四个目录
[root@linletao-001 nginx-1.12.1]# ls /usr/local/nginx/
conf html logs sbin
然后我们编辑一个启动脚本
- vim /etc/init.d/nginx
- #!/bin/bash
- chkconfig: - 30 21
description: http service.
- Source Function Library
- . /etc/init.d/functions
- Nginx Settings
- NGINX_SBIN="/usr/local/nginx/sbin/nginx"
- NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
- NGINX_PID="/usr/local/nginx/logs/nginx.pid"
- RETVAL=0
- prog="Nginx"
- start()
- {
- echo -n $"Starting $prog:"
- mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
- RETVAL=$?
- echo
- return $RETVAL
- }
- stop()
- {
- echo -n $"Stopping $prog:"
killproc -p $NGINX_PID $NGINX_SBIN -TERM
- rm -rf /dev/shm/nginx_temp
- RETVAL=$?
- echo
- return $RETVAL
- }
- reload()
- {
- echo -n $"Reloading $prog:"
killproc -p $NGINX_PID $NGINX_SBIN -HUP
- RETVAL=$?
- echo
- return $RETVAL
- }
- restart()
- {
- stop
- start
- }
- configtest()
- {
$NGINX_SBIN -c $NGINX_CONF -t
- return 0
- }
- case "$1" in
- start)
- start
- ;;
- stop)
- stop
- ;;
- reload)
- reload
- ;;
- restart)
- restart
- ;;
- configtest)
- configtest
- ;;
- *)
- echo $"Usage: $0 {start|stop|reload|restart|configtest}"
- RETVAL=1
- esac
- exit $RETVAL
也可以去网站直接复制, https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx
然后更改权限启动脚本的权限
- chmod 755 /etc/init.d/nginx
- chkconfig --add nginx
开机自启动
chkconfig nginx on
然后我们更改一下 nginx 的配置文件, 我们要将原来的配置文件清空.
首先我们进入 nginx 的配置文件目录
- cd /usr/local/nginx/conf/
然后将以前的配置文件清空
- /usr/local/nginx/conf/nginx.conf(> 重定向符号, 单独使用, 可以吧一个文档快速清空.)
然后编辑配置文件
- vim /usr/local/nginx/conf/nginx.conf
- user nobody nobody;
- worker_processes 2;
- error_log /usr/local/nginx/logs/nginx_error.log crit;
- pid /usr/local/nginx/logs/nginx.pid;
- worker_rlimit_nofile 51200;
- 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;
- }
- }
- }
也可以去网站直接复制 https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf
然后检查是否有语法错误
- [root@linletao-001 conf]# /usr/local/nginx/sbin/nginx -t
- nginx: the configuration file /usr/local{$82
}conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local{$84
}conf/nginx.conf test is successful
配置语法正确.
然后我们启动 nginx
- /etc/init.d/nginx start
启动后查看是否成功启动
- [root@linletao-001 logs]# ps aux |grep nginx
- root 5213 0.0 0.0 20496 620 ? Ss 22:10 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 5214 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
nobody 5215 0.0 0.3 22940 3208 ? S 22:10 0:00 nginx: worker process
root 5219 0.0 0.0 112676 976 pts/0 R+ 22:10 0:00 grep --color=auto nginx
上面显示有两个 worker, 说明启动成功. 然后我们查看一下端口
- [root@linletao-001 logs]# netstat -lntp |grep 80
- tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5213/nginx: master
已经成功的监听了 80 端口.
然后我们看是否能解析 php
首先编辑文件
- vim /usr/local/nginx/html/1.php
然后输入内容
- <?php
- echo "this is nginx pape.";
最后用 curl 命令解析
[root@linletao-001 logs]# curl localhost/1.php
this is nginx pape.[root@linletao-001 logs]#
解析成功.
Nginx 默认虚拟主机
首先先改变一下 nginx.conf 的配置文件
- vim /usr/local/nginx/conf/nginx.conf
然后将配置文件中 service 下面的内容全部删掉.
这里需要注意的是} 的保留, 只留最后一个.
然后再最后增加一行文字
然后进入{$95
}local/nginx/conf
增加一个子目录 vhost
然后进入这个子目录, 创建一个文件, 如 aaa.com.conf
然后将下面的内容复制到新创建的文件中.
- server
- { listen 80 default_server;
- server_name aaa.com; index index.html index.htm index.php;
- root /data/wwwroot/default;
- }
保存退出
然后创建 mkdir -p{$98
}wwwroot/default/
并在下面输入内容
- echo "This is a default site.">/data/wwwroot/default/index.html
然后检查语法是否错误
- /usr/local/nginx/sbin/nginx -t
- nginx: the configuration file /usr/local{$105
}conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local{$107
}conf/nginx.conf test is successful
然后创建浏览器的根目录
- mkdir -p /data/wwwroot/default/
然后我么定义一个文件
vim index.html
输入一个内容
This is the default site.
保存后检查语法错误
- root@linletao-001 default]# /usr/local/nginx/sbin/nginx -t
- nginx: the configuration file /usr/local{$111
}conf/nginx.conf syntax is ok
- nginx: configuration file /usr/local{$113
}conf/nginx.conf test is successful
语法没有错的话我们就重新加载一下
- /usr/local/nginx/sbin/nginx -s reload
然后测试
[root@linletao-001 default]# curl localhost
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 123.com
This is the default site.
[root@linletao-001 default]# curl -x127.0.0.1:80 bbb.com
This is the default site.
测试成功.
Nginx 用户认证
首先我们先来创建一个虚拟主机
- vim /usr/local/nginx/conf/vhost/test.com.conf
然后输入下面的内容.
- 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; (用户名密码文件)
- }
- }
我们以后在工作时可以直接复制, 但是要注意 server_name 和 root 的不同.
然后我们生成用户名密码文件. 如果我们有 apache 的话我们可以直接用它的密码生成文件, 如果没有, 我们可以 yum 先安装一个 apaceh, 然后我们再去用它.
- htpasswd -c /usr/local{$123
}conf/htpasswd aming(-c 是生成的意思. 如果想要第二个, 则不用再加 - c)
这里我们将用户密码发到 aming 这个文件中.
然后我们还是去检车语法错误和从新载入
- /usr/local/nginx/sbin/nginx -t
- /usr/local/nginx/sbin/nginx -s reload
然后我么做测试
[root@linletao-001 vhost]# curl -x127.0.0.1:80 test.com
- <html>
- <head><title>401 Authorization Required</title></head>
- <body bgcolor="white">
- <center><h1>401 Authorization Required</h1></center>
- <hr><center>nginx/1.12.1</center>
- </body>
- </html>
这里提示 401, 让我们 - u 指定用户.
[root@linletao-001 vhost]# curl -uaming:19860127 -x127.0.0.1:80 test.com
- <html>
- <head><title>404 Not Found</title></head>
- <body bgcolor="white">
- <center><h1>404 Not Found</h1></center>
- <hr><center>nginx/1.12.1</center>
- </body>
- </html>
这里提示 404, 是因为我们还没有创建它的主目录. 下面我们来创建它的主目录.
- mkdir /data/wwwroot/test.com
- echo "test.com">/data/wwwroot/test.com/index.html
然后我们再访问一遍.
- [root@linletao-001 vhost]# curl -uaming:19860127 -x127.0.0.1:80 test.com
- "test.com"
这样就正常了.
还有一种情况就是我们需要访问一个目录时才去认证, 比如我们要他访问 admin 时需要认证, 那我们可以这样做
在配置文件 vim{$137
- }local/nginx/conf{$139
}test.com.conf 中的 location / 后面直接接 admin,location / admin 直接改目录名就可以了.
这样在我们访问 test.com 时就不用输入密码了. 而要访问 admin 时就要输入密码. 否则会就会报 401.
Nginx 域名重定向
Nginx 的域名重定向和 httpd 类似. 首先我们改一下配置文件
- vim /usr/local/nginx/conf/vhost/test.com.conf
然后再 server_name 后面增加一些域名
server_name test.com test2.com test3.com
但是这样一来权重就变了, 为了不让权重出现变化, 那么我们要在配置文件中增加一行配置
if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent;
它的意思是如果域名不是 test.com, 我要做一个跳转.
然后我们做一个测试
[root@linletao-001 vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Tue, 24 Apr 2018 16:08:27 GMT
- Content-Type: text/html
- Content-Length: 185
- Connection: keep-alive
- Location: http://test.com/index.html
这样不管我们输入什么样的域名, 他都会跳转到我们预先设定好的 tes.com.
但是我们如果输入一个未知域名的话, 它就会访问到我们的默认虚拟主机上
来源: http://www.bubuko.com/infodetail-2574002.html