一, 安装相关
安装:
- tar zxvf nginx-1.2.9.tar.gz #解压 nginx
- cd nginx-1.2.9 #进入目录
- ./configure --prefix=/opt/soft/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module #配置安装模块
- make install #安装
--prefix: 指定安装目录, 默认的安装目录是 / usr/local/nginx; --with-http_ssl_module: 安装 https 服务模块
启动:
- /opt/soft/nginx/sbin/nginx
- /opt/soft/nginx/sbin/nginx -s stop # fast shutdown
- /opt/soft/nginx/sbin/nginx -s quit # graceful shutdown
- /opt/soft/nginx/sbin/nginx -s reload # reloading the configuration file
- /opt/soft/nginx/sbin/nginx -s reopen # reopening the log files
设置开机自启动:
echo "/opt/soft/nginx/sbin/nginx -c /opt/soft/nginx/conf/nginx.conf">> /etc/rc.local
Nignx 配置虚拟主机, 反向代理, 负载均衡
虚拟主机
主要配置 server 模块的 listen 和 server_name
基于域名
- server {
- listen 80;
- server_name test.a.com;
- location / {
- proxy_pass http://192.168.0.1; #反向代理到其他站点
- }
- }
- server {
- listen 80;
- server_name test.b.com;
- location / {
- proxy_pass http://192.168.0.2; #反向代理到其他站点
- }
- }
注意: 配置文件下载服务器
- server {
- listen 80;
- server_name file.download.com;
- charset utf-8;
- location ~ ^/(.*)$ {
- add_header Content-Disposition "attachment; filename=$1"; #设置 header
- alias "C:/Robot_Download/$1"; #文件的本地位置
- }
- }
基于端口
- server {
- listen 80;
- server_name localhost;
- alias /data/html/index.HTML; #也可使用 root,location 等方式指向静态资源
- }
- server {
- listen 81;
- server_name localhost;
- root /data/HTML/index.HTML; #也可使用 alias,location 等方式指向静态资源
- }
基于 ip
- server {
- listen 100.100.100.100:80;
- server_name localhost;
- location / {
- alias /data/HTML/index.HTML; #也可使用 alias,root 等方式指向静态资源
- }
- }
- server {
- listen 100.100.100.101:80;
- server_name localhost;
- location / {
- alias /data/HTML/index.HTML; #也可使用 alias,root 等方式指向静态资源
- }
- }
反向代理
主要配置 location 模块的 proxy_pass
- server {
- listen 80;
- server_name test.b.com;
- location / {
- proxy_pass http://192.168.0.2; #反向代理到其他应用服务器或 web 服务器
- }
- }
负载均衡
主要配置 upstream 和 location 模块的 proxy_pass
- upstream tomcat_server_pool{
- ip_hash;
- server 127.0.0.1:8090 weight=10; #设置访问权重, 权重越高越容易被访问
- server 127.0.0.1:8100 weight=10;
- server 127.0.0.1:8110 weight=7;
- }
- server {
- listen 80;
- server_name test.b.com;
- location / {
- proxy_pass http://tomcat_server_pool; #反向代理到其他服务器集合
- }
- }
**ip_hash:** 使用 ip_hash 策略的负载均衡解决 session 问题. 每个请求按访问 ip 的 hash 结果分配, 这样每个访客固定访问一个后端服务器, 可较好地解决 session 的问题.
location 映射规则
alias 和 root 的区别
- location /SVN/ {
- root /data/ftp/;
- autoindex on;
- }
访问 127.0.0.1/SVN/a.jpg: 则会进入到 /data/ftp/SVN/a.jpg
- location /SVN/ {
- alias /data/ftp/;
- autoindex on;
- }
访问 127.0.0.1/SVN/a.jpg: 则会进入到 /data/ftp/a.jpg
proxy_pass 的 url 是否存在 / 的区别
注意: alias 和 root 后的 url 都是要加 / 的
- location /proxy/ {
- proxy_pass http://127.0.0.1/;
- }
访问: 127.0.0.1/proxy/a.jpg: 则会请求到: http://127.0.0.1/a.jpg
- location /proxy/ {
- proxy_pass http://127.0.0.1;
- }
访问: 127.0.0.1/proxy/a.jpg: 则会请求到: http://127.0.0.1/proxy/a.jpg
"location /xxx/" 与 "location ^~ /xxx/" 区别
- location = / { #表示匹配访问根目录
- root HTML; #当前安装目录下的 HTML,/HTML 则表示服务器根目录下的 HTML
- index index.HTML index.htm;
- }
- location /SVN/ { #表示匹配 ip:port/SVN/
- root /data/;
- autoindex on;
- }
- }
- location ^~ /SVN/ { #表示只要含有 SVN / 就会被匹配
- root /data/;
- autoindex on;
- }
"location /xxx/" 表示匹配 ip:port/xxx
能匹配到 http://test.com/ xxx/home.jpg;
不能匹配到 http://test.com/folder/ xxx/home.jpg;
如果需要匹配到后者, 应改为:
location /folder/xxx/** 科普:** 一般默认都有 location = /, 如果不是 location = /(精确匹配), 而是 location /(模糊匹配), 模糊匹配和精确匹配的区别是, 模糊匹配就算匹配到也会一直匹配下去, 而精确匹配不会. 例: 如上配置 location = / 换成 location = /, 那么请求 112.74.55.239/SVN/, 则先会匹配 /, 实际请求路径就会是:/usr/local/nginx/HTML, 然后继续匹配到第二个 location, 故, 又加上 /data/SVN/! 因此, 实际访问路径是 /usr/local/nginx/HTML/data/SVN/
- rewrite
- location ~ \.PHP${
- rewirte "^/php/(.*)$" http://localhost:8090/$1
- }
将
localhost/PHP/test.PHP
重定向到
localhost:8090/test.PHP
. 如果正则表达式 (regex) 匹配到了请求的 URI(request URI), 这个 URI 会被后面的 replacement 替换
如果正则表达式 (regex) 里包含 "}" or ";" 字符, 需要用单引号或者双引号把正则表达式引起来
如果 replacement 字符串里有新的 request 参数, 那么之前的参数会附加到其后面, 如果要避免这种情况, 那就在 replacement 字符串后面加上 "?",eg: rewrite ^/users/(.*)$ /show?user=$1? last;=
可选的 flag 参数如下:
last
结束当前的请求处理, 用替换后的 URI 重新匹配 location;
可理解为重写 (rewrite) 后, 发起了一个新请求, 进入 server 模块, 匹配 location;
如果重新匹配循环的次数超过 10 次, nginx 会返回 500 错误;
返回 302 http 状态码 ;
浏览器地址栏显示重定向后的 url
break
结束当前的请求处理, 使用当前资源, 不在执行 location 里余下的语句;
返回 302 http 状态码 ;
浏览器地址栏显示重定向后的 url
redirect
临时跳转, 返回 302 http 状态码;
浏览器地址栏显示重定向后的 url
permanent
永久跳转, 返回 301 http 状态码;
浏览器地址栏显示重定向后的 url
常用配置
支持后端获取真实客户端 IP, 而不是该代理的 IP
- proxy_set_header Host $http_host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header REMOTE-HOST $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
解决 request.getScheme()获取不到真实协议
proxy_set_header X-Forwarded-Proto $scheme;
websocket 配置
- map $http_upgrade $connection_upgrade {
- default upgrade;
- '' close;
- }
来源: https://juejin.im/post/5c1c52d46fb9a049b347f3e3