nginx命令./nginx -s reloadnginx -vnginx -V 包括查看安装的模块nginx -s stop 立即停止nginxnginx -s quit 等待到请求被处理完成后停止nginxnginx 启动nginxh1指令块/h1location块配置请求的路由以及各种页面的处理情况。http块可以嵌套多个server块。h1rewrite 与 location/h1rewrite和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:8080server 127.0.0.1:8081}server{listen 80;location / {proxy_pass http://backend;}}}nginx反向代理默认情况下会轮询后台应用而如果加了ip_hash标志则固定客户端总是会被反向代理到固定的一个服务上即同一客户端ip会被分配给固定的后端服务器这样可以解决session问题。代理配置前端服务中配置反向代理请求被传递到被代理地址。配置文件位于/usr/local/nginx/conf/vhostserver{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 weight5 max_fails3 fail_timeout30s; } server { listen 3306;#数据库服务器监听端口 proxy_connect_timeout 10s; proxy_timeout 300s;#设置客户端和代理服务之间的超时时间如果5分钟内没操作将自动断开。 proxy_pass cloudsocket; } }stream和http是同级别的不要放入http里面。如果重启nginx报错unknown directive stream则可通过动态加载stream模块解决。下载相同版本nginxhttp://nginx.org/download/nginx-1.10.1.tar.gz解压。通过 ./nginx -V 查看configure arguments。配置 ./configure 原参数在最后加上--with-stream --with-streamdynamicmake拷贝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。配置静态图片地址#静态图片目录location /details/images {alias D:/Pictures/dev_test; # 设置外部目录的路径autoindex on; # 可选开启目录浏览}例如请求 http://127.0.0.1/details/images/abc/2.jpg可访问到D:/Pictures/dev_test/abc/2.jpg。注意location 中最后的反斜杠要与alias中最后的反斜杠一致都有或者都没有。