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/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/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下面的内容全部删掉。
这里需要注意的是}的保留,只留最后一个。
然后再最后增加一行文字
include vhost/*.conf;
然后进入/usr/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 /data/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/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/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/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/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/nginx/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>401 Authorization Required</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>404 Not Found</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 /usr/local/nginx/conf/vhost/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。 但是我们如果输入一个未知域名的话,它就会访问到我们的默认虚拟主机上