cd /usr/local/srcjavascript
wget http://nginx.org/download/nginx-1.12.1.tar.gz =nginx下载地址(或者直接能够去官网下载)php
tar zxf nginx-1.12.1.tar.gz = 解压下载包css
cd nginx-1.12.1 = 进入解压好的目录html
./configure --prefix=/usr/local/nginx =编译nginx (编译时能够根据需求添加须要的模块)java
make && make install = 继续安装node
vim /etc/init.d/nginx //复制以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx?public=true ) = 编辑启动脚本linux
#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
chmod 755 /etc/init.d/nginx = 给予启动脚本权限nginx
chkconfig --add nginx = 加入开机服务自启动git
chkconfig nginx on = 开启开机服务自启动 on=开启 off=关闭vim
cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak = 进入nginx配置目录给本来的配置文件更改一个名字
vim nginx.conf //写入以下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf) = 写入本身的配置文件
#nginx配置文件 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; } } }
/usr/local/nginx/sbin/nginx -t = nginx查看自身语法是否有错误
/etc/init.d/nginx start = 启动nginx
netstat -lntp |grep 80 = 查看端口 nginx监听80端口
systemctl status nginx.service = 若是启动nginx 出问题能够使用命令来判断问题点在哪
vi /usr/local/nginx/html/1.php //加入以下内容
<?php
echo "test php scripts.";
?>
curl localhost/1.php
vim /usr/local/nginx/conf/nginx.conf //增长 = 更改原来的配置文件
include vhost/*.conf = 增长新定义的配置文件
mkdir /usr/local/nginx/conf/vhost =在conf目录下新建立一个vhost目录
cd !$; vim default.conf //加入以下内容 = vhost目录下default.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/ = 配置里面定义的网站根目录 (若是有那么就能够不用建立)
cd /data/wwwroot/default/ = 进入到建立的网站根目录随便写点东西测试使用
vim index.html =定义一个测试的配置文件 echo “This is a default site.”
echo “This is a default site.”>/data/wwwroot/default/index.html = 能够直接写入
/usr/local/nginx/sbin/nginx -t = 判断语法错误
/usr/local/nginx/sbin/nginx -s reload = 从新加载配置文件
curl localhost = 测试解析本机
curl -x127.0.0.1:80 123.com = 测试解析其余域名
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; =定义用户密码文件 } }
yum install -y httpd = 工具需求安装
htpasswd -c /usr/local/nginx/conf/htpasswd aming =生成一个用户名和密码文件(若是还须要继续生成第二个用户和密码那么就不用加 -c )
cat 能够查看用生成的用户和密码
-t && -s reload //测试配置并从新加载
[root@aming-01 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@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload = 从新加载配置文件
mkdir /data/wwwroot/test.com = 建立一个测试配置文件目录
echo “test.com”>/data/wwwroot/test.com/index.html = 建立的测试配置文件
curl -x127.0.0.1:80 test.com -I//状态码为401说明须要验证 = 测试 出现401说明须要认证用户
curl -uaming:passwd 访问状态码变为200 = 认证用户后测试状态为200
[root@aming-01 vhost]# curl -uaming:rabbit -x127.0.0.1:80 test.com “test.com”
编辑windows的hosts文件,而后在浏览器中访问test.com会有输入用户、密码的弹窗针对目录的用户认证
location /admin/ { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; }
更改test.com.conf
server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; } }
server_name后面支持写多个域名,这里要和httpd的作一个对比
permanent为永久重定向,状态码为301,若是写redirect则为302
nginx.conf 配置详解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag
http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943