nginx安装配置

准备工作:
    ①安装依赖:
        yum install -y gcc
        yum install -y pcre-devel
        yum install -y openssl openssl-devel
        yum install -y zlib-devel
    ②下载nginx:  http://nginx.org/en/download.html 


    ③解压nginx:   tar -zxf nginx-1.10.3.tar.gz 
    
    ④编译nginx:   ./configure 
    
    ⑤执行make命令:  make
    
    ⑥安装:    make install
        #执行完成后安装在了 /usr/local/nginx 目录下
启动:【在安装目录下执行】
    ①执行命令: ./sbin/nginx 
        #在浏览器访问  http://【nginx所在主机ip】
    ②退出:./sbin/nginx -s quit
    
    ③强制退出:  ./sbin/nginx -s stop
    
    ④校验配置文件是否合法:    ./sbin/nginx -t
    

    ⑤重新加载:  ./sbin/nginx -s reload

配置nginx:conf目录下的nginxconf文件

①实践

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;
    keepalive_timeout  65;


    #gzip  on;


    upstream tomcats {   
        #默认负载均衡策略为轮询  
        server 192.168.1.2:8081 weight=5 max_fails=2 fail_timeout=600s; 
        #server 192.168.1.2:8082 weight=5 max_fails=2 fail_timeout=600s;                 
    }  
    
    server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;
        charset utf-8;
        #access_log  logs/host.access.log  main;


        location / {


            proxy_pass http://tomcats ;
            #root   html;
            #index  index.html index.htm;
        }
        location  ~ .*\.(php|jsp|cgi)?$  {
                proxy_pass http://tomcats;
                proxy_redirect    off;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $http_host;
                proxy_next_upstream http_502 http_504 error timeout invalid_header;
        }
        
        location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
            root  /usr/local/nginx/html;
            expires 3d;
        }
        

#这里是将以/TestSession/app开头的路径全部重写为/TestSession/main.html 路径,实现访问转发
        location ^~/TestSession/app {
             rewrite (.*)  /TestSession/main.html break;
        }


        #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;
        #}
    }




    # 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;
    #    }
    #}


}


②配置项说明:

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;  
keepalive_timeout 65;  
  
#打开gzip压缩  
#gzip on;  
  
#设定请求缓冲  
client_header_buffer_size 1k;  
large_client_header_buffers 4 4k;  
  
#设定负载均衡的服务器列表  
upstream myproject {   
#weigth参数表示权值,权值越高被分配到的几率越大  
#max_fails 当有#max_fails个请求失败,就表示后端的服务器不可用,默认为1,将其设置为0可以关闭检查  
#fail_timeout 在以后的#fail_timeout时间内nginx不会再把请求发往已检查出标记为不可用的服务器  
#这里指定多个源服务器,ip:端口,80端口的话可写可不写   
server 192.168.1.78:8080 weight=5 max_fails=2 fail_timeout=600s;  
#server 192.168.1.222:8080 weight=3 max_fails=2 fail_timeout=600s;   
}  
  
#第一个虚拟主机  
server {  
#监听IP端口  
listen 80;  
  
#主机名  
server_name localhost;  
  
#设置字符集  
#charset koi8-r;  
  
#本虚拟server的访问日志 相当于局部变量  
#access_log logs/host.access.log main;   
  
#对本server"/"启用负载均衡  
location / {   
#root /root; #定义服务器的默认网站根目录位置  
#index index.php index.html index.htm; #定义首页索引文件的名称  
proxy_pass http://myproject; #请求转向myproject定义的服务器列表  
  
#以下是一些反向代理的配置可删除.  
# proxy_redirect off;   
# proxy_set_header Host $host;   
# proxy_set_header X-Real-IP $remote_addr;   
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
# client_max_body_size 10m; #允许客户端请求的最大单文件字节数   
# client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,   
# proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)   
# proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)   
# proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)   
# proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小   
# proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置   
# proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)   
# proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传  
}   
location /upload {   
alias e:/upload;   
}  
#设定查看Nginx状态的地址   
location /NginxStatus {   
stub_status on;   
access_log off;   
#allow 192.168.0.3;  
#deny all;  
#auth_basic "NginxStatus";   
#auth_basic_user_file conf/htpasswd;   
}  
  
#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;  
#}  
}   
  
  
# 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 / {  
#WEB文件路径  
# root html;  
#默认首页  
# index index.html index.htm;  
# }  
#}  
  
  
# HTTPS server HTTPS SSL加密服务器  
#  
#server {  
# listen 443;  
# server_name localhost;  
  
# ssl on;  
# ssl_certificate cert.pem;  
# ssl_certificate_key cert.key;  
  
# ssl_session_timeout 5m;  
  
# ssl_protocols SSLv2 SSLv3 TLSv1;  
# 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、付费专栏及课程。

余额充值