nginx

nginx命令

./nginx -s reload
nginx -v
nginx -V 包括查看安装的模块
nginx -s stop 立即停止nginx
nginx -s quit 等待到请求被处理完成后停止nginx
nginx 启动nginx

<h1>指令块</h1>
location块:配置请求的路由,以及各种页面的处理情况。
http块:可以嵌套多个server块。

<h1>rewrite 与 location</h1>
rewrite和location功能有点像,都能实现跳转。
区别:
rewrite是在同一域名内更改获取资源的路径,实现url重写和重定向,rewrite可放在server{},location{},if{}中。
location是对一类路径做控制访问或反向代理,location放在server{}中。
proxy_pass起到反向代理的作用,proxy_pass放在location{}中。

Nginx服务器中的重定向配置参考指南
http://www.link588.com/html/wangluobiancheng/139613.html

负载均衡

http{
        upstream backend1{
                ip_hash;
                server 127.0.0.1:8080
                server 127.0.0.1:8081
        }

        server{
                listen 80;
                location / {
                        proxy_pass http://backend;
                }
         }
}

nginx反向代理默认情况下会轮询后台应用,而如果加了ip_hash标志,则固定客户端总是会被反向代理到固定的一个服务上,即同一客户端ip会被分配给固定的后端服务器,这样可以解决session问题。 

代理配置

前端服务中配置反向代理,请求被传递到被代理地址。
配置文件位于:/usr/local/nginx/conf/vhost

server{
    listen 80;
    listen 443 ssl;

        #ssl_certificate /usr/local/nginx/conf/wwkssl.crt;
        #ssl_certificate_key /usr/local/nginx/conf/wwkssl.key;
        ssl_certificate /etc/letsencrypt/live/www.
xxx.cn/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.xxx.cn/privkey.pem;

        server_name xxx.cn www.xxx.cn;

 #nginx 对文档检测比较严格,所以if  ( $host != 'www.csdn.com'  ) 这些代码之间需要有空格隔开,不然会报错:unknown directive “if($host!=”
 if ( $host != 'www.xxx.cn' )
 {
   rewrite ^/(.*)$http://www.xxx.cn/$1 permanent;
 }

        #proxy_set_header X-Forwarded-Host $host;
        #proxy_set_header X-Forwarded-Server $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header Host $host:$server_port;

        location / {
                proxy_pass https://被代理地址;

                proxy_set_header Host $host;
                proxy_set_header X-Real-IP       $remote_addr;
                proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        }

        if ($server_port = 80) {
                return 497;
        }

        #让http请求重定向到https请求
        #https://$host$uri?$args;
        error_page 497 https://$host$uri;
 }

上面代码块中rewrite的作用是使不带www的域名重定向到带www的域名,其中

$host变量的值等于请求头中Host的值。当Host无效时就是处理该请求的server的名称。

permanent表示永久性重定向,请求日志中的状态码为301。

nginx 对文档检测比较严格,所以if ( $host != 'www.csdn.com' ) 这些代码之间需要有空格隔开,不然会报错:unknown directive “if($host!=”


在被代理服务器中配置如下:

server {
        listen 80;
        listen 443 ssl;
       

        #当同端口多域名时,server_name后要配置为具体域名
        server_name localhost;
        index index.html index.htm index.php;
        root /xxx/www/xxx;

        ssl_certificate /xxx/server/nginx/conf/wwkssl.crt;
        ssl_certificate_key /xxx/server/nginx/conf/wwkssl.key;

        location ~ .*\.(php|php5)?$
        {
                #fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 30d;
        }
        location ~ .*\.(js|css)?$
        {
                expires 1h;
        }
       
        include /xxx/server/nginx/conf/rewrite/wordpress.conf;
        access_log  /xxx/log/nginx/access/xxx.log;
}

nginx转发

nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。nginx.conf中配置如下:

stream {
    upstream cloudsocket {
       hash $remote_addr consistent;
       server 192.168.182.155:3306 weight=5 max_fails=3 fail_timeout=30s;
    }
    server {
       listen 3306;#数据库服务器监听端口
       proxy_connect_timeout 10s;
       proxy_timeout 300s;#设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
       proxy_pass cloudsocket;
    }
}

stream和http是同级别的,不要放入http里面。

如果重启nginx报错:unknown directive "stream",则可通过动态加载stream模块解决。 

  • 下载相同版本nginx,http://nginx.org/download/nginx-1.10.1.tar.gz,解压。
  • 通过 ./nginx -V 查看configure arguments。
  • 配置 ./configure 原参数,在最后加上--with-stream --with-stream=dynamic
  • make
  • 拷贝objs/ngx_stream_module.so 到 /usr/local/nginx/modules/ngx_stream_module.so
  • 在nginx.conf中event行前加入  load_module /usr/local/nginx/modules/ngx_stream_module.so; 重启nginx。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值