nginx安装
- cd /usr/local/src/
- wget http://nginx.org/download/nginx-1.14.0.tar.gz
- tar zxfv nginx-1.14.0.tar.gz
- cd nginx-1.14.0/
- ./configure --prefix=/usr/local/nginx
- make && make install
- 启动文件配置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
- 开机启动
- chmod 755 /etc//init.d/nginx
- chkconfig --add nginx
- chkconfig nginx on
- nginx的配置文件
[root@aminglinux-02 conf]# mv nginx.conf nginx.conf.bak [root@aminglinux-02 conf]# vim nginx.conf
参考
user nobody nobody; #定义Nginx运行的用户和用户组 worker_processes 2; #nginx进程数,建议设置为等于CPU总核心数 error_log /usr/local/nginx/logs/nginx_error.log crit; #全局错误日志定义类型 pid /usr/local/nginx/logs/nginx.pid; #进程文件 worker_rlimit_nofile 51200; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。 events #工作模式与连接数上限 { use epoll; worker_connections 6000; #单个进程最大连接数(最大连接数=连接数*进程数) } http #设定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$ #php解析 { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; #fastcgi_pass 127.0.0.1:9000; #指定监听的sockter或者Ip加端口 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
- 检测配置文件/usr/local/nginx/sbin/nginx -t
- 重新加载配置文件/usr/local/nginx/sbin/nginx -s reload
- 测试
- curl localhost
- 测试php解析
[root@aminglinux-02 ~]# vim /usr/local/nginx/html/2.php [root@aminglinux-02 ~]# curl localhost/2.php test php scripts.
nginx的默认虚拟主机
- 在/usr/local/nginx/conf/nginx.conf的http服务里面增加一行 include vhosts/*.conf;
- mkdir /usr/local/nginx/conf/vhost
- vim /usr/local/nginx/conf/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
- 创建测试页
[root@aminglinux-02 vhosts]# vim /data/wwwroot/default/index.html this is a default site.
- /usr/local/nginx/sbin/nginx -t
- /usr/local/nginx/sbin/nginx -s reload
- 测试
[root@aminglinux-02 vhosts]# curl localhost this is a default site. [root@aminglinux-02 vhosts]# curl -x 127.0.0.1:80 aaa.com this is a default site. [root@aminglinux-02 vhosts]# curl -x 127.0.0.1:80 bbb.com this is a default site.
nginx的用户认证
-
vim /usr/local/nginx/conf/vhosts/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目录,~admin.php针对一个页面 { auth_basic "Auth" #指定用户名 auth_basic_user_file /usr/local/nginx/conf/htpasswd; #指定密码文件 } }
- yum install -y httpd 安装apche
Downloading packages: Error downloading packages: mailcap-2.1.41-2.el7.noarch: [Errno 5] [Errno 12] Cannot allocate memory httpd-tools-2.4.6-80.el7.centos.x86_64: [Errno 5] [Errno 12] Cannot allocate memory apr-util-1.5.2-6.el7.x86_64: [Errno 5] [Errno 12] Cannot allocate memory httpd-2.4.6-80.el7.centos.x86_64: [Errno 5] [Errno 12] Cannot allocate memory apr-1.4.8-3.el7_4.1.x86_64: [Errno 5] [Errno 12] Cannot allocate memory 这个错误需要手动安装不能安装的包,从下网上一个个安装
- 生产密码文件
[root@aminglinux-02 ~]# htpasswd -c /usr/local/nginx/conf/htpasswd aming New password: Re-type new password: Adding password for user aming [root@aminglinux-02 ~]# cat /usr/local/nginx/conf/htpasswd aming:$apr1$Th4uOUg/$tKN0B6BveSJ2.DtPu4Yfl. [root@aminglinux-02 ~]# htpasswd /usr/local/nginx/conf/htpasswd aming01 New password: Re-type new password: Adding password for user aming01 [root@aminglinux-02 ~]# cat /usr/local/nginx/conf/htpasswd aming:$apr1$Th4uOUg/$tKN0B6BveSJ2.DtPu4Yfl. aming01:$apr1$6aaY/ylb$9eUuW8lFsxlbzcnVQUHvq1
- 重新加载配置文件
[root@aminglinux-02 ~]# /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@aminglinux-02 ~]# /usr/local/nginx/sbin/nginx -s reload
- 测试
[root@aminglinux-02 ~]# curl -u aming:123456 -x127.0.0.1:80 test.com test.com
域名重定向
- 更改test.com.conf
server { listen 80; server_name test.com test1.com test2.com; #指定多个域名,httpd不一样 index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com') { rewrite ^/(.*)$ http://test.com/$1 permanent; #permanent是301,redirect是3 02 } }
- 加载配置
[root@aminglinux-02 ~]# /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@aminglinux-02 ~]# /usr/local/nginx/sbin/nginx -s reload
- 测试
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 test1.com/index.html/daad -I HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Fri, 08 Jun 2018 01:39:47 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/index.html/daad [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test2.com/index.html/daad -I HTTP/1.1 301 Moved Permanently Server: nginx/1.6.2 Date: Fri, 08 Jun 2018 01:39:53 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://test.com/index.html/daad [root@aminglinux-02 ~]# curl -x127.0.0.1:80 test3.com/index.html/daad -I HTTP/1.1 404 Not Found Server: nginx/1.6.2 Date: Fri, 08 Jun 2018 01:39:58 GMT Content-Type: text/html Content-Length: 168 Connection: keep-alive
#扩展
- nginx.conf 配置详解( http://my.oschina.net/duxuefeng/blog/34880)
- nginx rewrite四种flag(http://unixman.blog.51cto.com/10163040/1711943)