文章目录
- Nginx简介
- 反向代理
- 在Mac上安装Ngnix
- Nginx启动或关闭过程中的问题
- Tomcat + Nginx
- 需求
- 启动tomcat
- 启动nginx
- 配置文件
- 拦截部分
- 拦截一
- 拦截二
- 结果展示
- Nginx负载均衡
- 需求
- 启动tomcat
- 启动nginx
- 配置文件
- 域名和IP地址映射
- upstream字段
- 拦截
- 结果展示
Nginx简介
是异步框架的 Web服务器,也可以用作反向代理,负载平衡器 和 HTTP缓存。该软件由 Igor Sysoev 创建,并于2004年首次公开发布。[6] 同名公司成立于2011年,以提供支持。
反向代理
用户访问代理服务器,从代理服务器访问后台的服务器,如图:
在Mac上安装Ngnix
参考链接:mac下nginx的安装和配置
Nginx启动或关闭过程中的问题
如果stop报如下错误:
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
尝试reload报如下错误:
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
启动nginx报:
[emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
总之就是死循环,可以尝试使用
sudo lsof -i -P | grep -i "80"
找到nginx的进程号,给它杀死
例如
使用kill 26166
即可杀死,之后重新使用nginx启动即可
Tomcat + Nginx
需求
监听url,跳转到tomcat中
启动tomcat
略
启动nginx
略
配置文件
#user nobody;
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;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location = /yaoyan {
proxy_pass http://localhost:8080;
}
location ~ \.jsp$ {
index index.jsp;
proxy_pass http://localhost:8080;
}
location ~ \.(html|js|css|gif|jpg|png|bmp|swf)$ {
proxy_pass http://localhost:8080;
root /Users/xiehanchao/Downloads/apache-tomcat-7.0.92/webapps/ROOT;
}
#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;
index index.html index.htm;
}
# 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;
# }
#}
include servers/*;
}
拦截部分
location后面的内容可以拦截网址,也可以加入正则表达式
拦截一
location = /yaoyan {
proxy_pass http://localhost:8080;
}
可以在tomcat的ROOT目录中创建yaoyan.html
在网页中访问链接,即可进行反向代理
//监听端口号
listen 80;
//对应url地址
server_name localhost;
拦截二
输入localhost:80/index.jsp反向代理到localhost:8080/index.jsp
location ~ \.jsp$ {
proxy_pass http://localhost:8080;
}
//这个拦截目的是为了tomcat里面的图片资源可以正常进行加载
location ~ \.(html|js|css|gif|jpg|png|bmp|swf)$ {
proxy_pass http://localhost:8080;
root /Users/xiehanchao/Downloads/apache-tomcat-7.0.92/webapps/ROOT;
}
结果展示
Nginx负载均衡
需求
浏览器输入 http://www.wenrouqizhinichaoge.com:80 跳转到tomcat首页
启动tomcat
略
启动nginx
略
配置文件
#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;
keepalive_timeout 65;
#gzip on;
upstream balance_server {
server 172.24.55.10:8080;
}
server {
listen 80;
server_name www.wenrouqizhinichaoge.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.jsp;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://balance_server;
}
#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;
index index.html index.htm;
}
# 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;
# }
#}
include servers/*;
}
域名和IP地址映射
cd /private/etc/目录
vim hosts
在文件的末尾添加一条
upstream字段
该字段为反向代理的服务器名称,我写的是我ip地址
upstream balance_server {
server 172.24.55.10:8080;
}
拦截
listen 80;
server_name www.wenrouqizhinichaoge.com;
location / {
index index.jsp;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://balance_server;
}
结果展示