Linux 安装nginx 1.8.1 及配置

1.创建 /usr/local/nginx

2.wget http://nginx.org/download/nginx-1.11.13.tar.gz

3.

[root@Server1 nginx-1.8.1]# ./configure  --prefix=/usr/local/nginx  --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log  --http-log-path=/var/log/nginx/access.log  --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock  --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
4.生成脚本及配置文件:make
5.安装:make install


 
 
复制代码
[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginx
nginx: [emerg] getpwnam("nginx") failed  #没有nginx用户
解决:useradd -s /sbin/nologin -M nginx


[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginx
nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)  #目录不存在
解决:创建var/tmp/nginx/client/  目录


[root@Server1 sbin]# /usr/local/nginx/sbin/nginx/nginx  #直到没有报错,才算启动完成
复制代码

9、重读配置文件和关闭服务:

[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx  #启动 服务
[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx   -s  reload  #不停止服务重读配置文件
[root@Server1 local]# /usr/local/nginx/sbin/nginx/nginx -s stop #停止服务  #停止服务

10.验证端口是否开启:

复制代码
[root@Server1 sbin]# ps -ef | grep nginx
root     13228     1  0 Apr23 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx/nginx  #nginx的主进程,只有一个主进程
nginx    13229 13228  0 Apr23 ?        00:00:00 nginx: worker  process #nginx工作进程,默认只有一个,可以通过修改nginx.conf中的worker_processes  1; 参数启动多个工作进程
root     13295  1400  0 00:01 pts/0    00:00:00 grep --color=auto nginx

[root@Server1 local]# lsof -i:8090  #显示占用8090的进程
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 13337 root 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)
nginx 13338 nginx 6u IPv4 5932680 0t0 TCP *:8090 (LISTEN)
复制代码

 11、通过给nginx的主进程ID号发送信号启动或停止nginx:

获取nginx主进程号的办法:

[root@Server1 nginx]# cat /var/run/nginx/nginx.pid   #查看nginx的pid文件,此文件保存的就是nginx的主进程id
13337  #次ID是随机的,每次启动都不一样的
[root@Server1 nginx]# ps -ef   | grep nginx  #过滤nginx的进程号
root     13337     1  0 00:05 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx/nginx
nginx    21568 13337  0 10:58 ?        00:00:00 nginx: worker process

支持的信号:

复制代码
[root@Server1 nginx]# kill  -QUIT 13337  #平缓关闭Nginx,即不再接受新的请求,但是等当前请求处理完毕后再关闭Nginx。
[root@Server1 nginx]# kill  -TERM  21665 #快速停止Nginx服务
[root@Server1 nginx]# kill  -HUP 21703 #使用新的配置文件启动进程然后平缓停止原有的nginx进程,即平滑重启。 
[root@Server1 nginx]# kill -USR1 21703   #重新打开配置文件,用于nginx 日志切割
日期切割的脚本:
#!/bin/bash
PID=`cat /var/run/nginx/nginx.pid`
mv   /var/log/nginx/access.log   /var/log/nginx/`date  +%Y_%m_%d:%H:%M:%S`.access.log
kill -USR1 $PID
[root@Server1 nginx]# kill -USR2 21703   #使用新版本的nginx文件启动服务,然后在平缓停止原有的nginx服务,即平滑升级。
[root@Server1 nginx]# kill -WINCH  21703  #平滑停止nginx的工作进程,用于nginx平滑升级。
复制代码

 

三:nginx 主配置文件:nginx.conf

3.1:默认配置:配置文件默认保存在path/conf当中,默认的配置文件为nginx.conf,以下是编译安装后的默认配置:

复制代码
[root@Server1 conf]# grep -v "#" nginx.conf | grep -v  "^$"
 


user  root;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




http {
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;


    gzip            on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 5;
 gzip_types text/plain  text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
    gzip_vary         on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65; 
   
    client_max_body_size 10M; 
    
    client_header_timeout 300s;
    client_body_timeout 600s;


   upstream crm{
   	ip_hash;
	server 172.16.217.17:8080;
	
   }
    server {
        listen       8001;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        
      location / {
	          proxy_pass http://crm;
	          proxy_redirect default;
	      }
        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
	    


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
	
	 upstream protal{
		   	ip_hash;
			server 172.16.217.17:9001;
	   }


		 server {
        listen       9001;
        server_name  localhost;     
	
	 		  location / {
	          proxy_pass http://protal;
	          proxy_redirect default;
	      }
	
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    upstream gateway{
		   	ip_hash;
			server 172.16.217.15:8080;
			server 172.16.217.16:8080;
			
	}


		 server {
        	listen       7001;
        	server_name  localhost;     
	 	
	 		  location / {
	          proxy_pass http://gateway;
	          proxy_redirect default;
	      }
	
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }




    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值