配置Nginx作为反向代理服务器
最近在实习公司的开发一个项目,项目是前后端彻底分离的项目,前端项目和后端项目各监听着特定的端口号,显然不是80的通用端口,为了不在地址栏上输入IP+端口号的形式,我们可以使用Nginx作为反向代理服务器,达到人性化访问的目的。
一,设置hosts
当然首先别忘了设置hosts,把你想要的域名设置进去,设置方法如下:
cd /etc
sudo vi hosts
//hosts文件
//添加示例如下:
127.0.0.1 www.example.com
这个时候你访问www.example.com就是访问的127.0.0.1:8080这个url
好了,设置好了hosts,我们需要配置Nginx反向代理服务器的配置。
二,配置Nginx反向代理服务器
首先我们进入Nginx的目录。
cd /usr/local/etc/nginx
我们会发现这个目录下有个nginx.conf的配置文件
#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 8080;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #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;
# }
#}
include servers/*;//主要看这里,这里的配置说明了除了可以在这里配置Nginx的配置外,还可以在当前目录下的servers子目录中配置。
}
那么我们去servers目录下配置我们的属性。
upstream example_server_name {
server localhost:8081; //这里配置我们的前端地址,其实这里可以配置多个地址那么Nginx做的不光是反向代理的功能了,还能实现负载均衡的功能。
}
这样设置好了我们的前端地址,那么我们需要把访问hosts设置的地址转向访问我们上面设置的前端地址。
server { listen 80; //这里国际默认通用80端口
server_name www.example.com *.example.com;//hosts中设置的访问地址 # individual nginx logs for this web vhost
access_log /tmp/rrs-access.log;
error_log /tmp/rrs-error.log ; location = /favicon.ico {
return 404;
} #when not specify request uri, redirect to /index;
location = / {
rewrite ^ /index ;
} #static files
location ~/(.*)$ {
root x/xx/xx; //静态资源访问
expires 30d;
access_log off;
} location / {
proxy_pass http://example_web_server; //配置转向的upstream,就是最上面设置的upstream。
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-Mobile-Client $mobile_request;
}
}
这个时候Nginx反向代理服务器的配置就已经配置完成了,我们可以访问www.example.com验证一下Nginx代理是否成功。
三,Nginx命令
sudo nginx //启动Nginx
sudo nginx -s reload //重启nginx
sudo nginx -s quit //退出nginx
我也是刚接触到Nginx很多东西也不是很熟悉,慢慢学习以后不断补充......
配置Nginx作为反向代理服务器的更多相关文章
- 【大型网站技术实践】初级篇:借助Nginx搭建反向代理服务器
一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从 ...
- Nginx搭建反向代理服务器过程详解
一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从 ...
- 【翻译】使用nginx作为反向代理服务器,uWSGI作为应用服务器来部署flask应用
最近在看关于Docker和Nginx方面的内容,先于在Docker上开发以及部署python应用自然要先能够在本机上部署,其中找到一篇文章写的最为详细并且实验成功,所以在此翻译转载过来以备后需.[原文 ...
- Nginx搭建反向代理服务器
[大型网站技术实践]初级篇:借助Nginx搭建反向代理服务器 一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受int ...
- Nginx 搭建反向代理服务器过程详解
1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet ...
- 【转】Nginx搭建反向代理服务器过程详解
阅读目录 1.1 反向代理初印象 1.2 反向代理的作用 2.1 Nginx是神马? 2.2 Nginx的应用现状 2.3 Nginx的核心特点 3.1 准备一个ASP.NET网站部署到IIS服务器集 ...
- 借助Nginx搭建反向代理服务器
一.反向代理:Web服务器的"经纪人" 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网 ...
- docker配置nginx做反向代理管理tomcat应用
由于业务开始复杂,单一tomcat已经不足以满足业务需求,多tomcat部署起来不方便而且面临域名解析问题,因此开始增加反向代理,由于docker的易用性,便使用docker管理各个应用. docke ...
- Nginx用作反向代理服务器
Nginx作为反向代理服务器时转发请求的流程 客户端请求处理 当客户端请求来时,Nginx并不会立刻转发到上游服务器,而是想完整的接收到Nginx所在的服务器, 然后再把缓存的客户端的请求转发到上游服 ...
随机推荐
- Linux命令之pip
使用pip进行install,sudo pip install xxx 使用pip进行update,sudo pip install --update xxx 使用pip设置超时时间,sudo pip ...
- JAVA三框架工作原理是什么?
一.struts的工作原理: 1.初始化,读取struts-config.xml.web.xml等配置文件(所有配置文件的初始化) 2.发送HTTP请求,客户端发送以.do结尾的请求 3.填充Form ...
- sql server安装教程(2008 R2,图形界面安装/命令提示符安装即静默安装)
转自:http://blog.51cto.com/jimshu/585023 SQL Server 2008(32/64位)下载地址: 链接:https://pan.baidu.com/s/1eR5b ...
- mysql 建立表之间关系 一对一 练习2
创建db5数据库 create database db5 charset=utf8; use db5; 例二:一个管理员唯一对应一个用户 用户表: id user password 1 egon xx ...
- 001-project基本使用
一.概述 Project工具一般用来管理一个项目,制定项目的执行计划.这个项目可以是临时性的工作,可以是IT项目.工程类项目,也可是结婚这一事情,项目的特点是产生唯一性的成果或最终结果. 项目的三要素 ...
- Matplot相关(二)——统计图
Matplotlib:其能够支持所有的2D作图和部分3D作图.能通过交互环境做出印刷质量的图像. ————————缩写定义———————— import matplot.pyplot as plt — ...
- SQLAlchemy_定义(一对一/一对多/多对多)关系
目录 Basic Relationship Patterns One To Many One To One Many To Many Basic Relationship Patterns 基本关系模 ...
- 开发者需要知道的iOS 12
总体概况 iOS 12总体来看是对现有iOS的一次改进,并没有太多突破性的功能或者框架,但是Apple在底层做了很多优化的工作,优化了性能,提供了更强大的安全性,增强了AR.Siri体验,让人工智能更 ...
- shell文件安全与权限 笔记
主要学习: 文件盒目录的权限 Setuid Chown和chgrp Umask 连接符号 一个文件已经创建,就具有三种访问方式 读,可以显示该文件的内容 写,可以编辑或删除它 执行,如果该文件时一个s ...
- shiro的Realm
public class UserRealm extends AuthorizingRealm { private UserService userService = new UserServiceI ...