linux下部署Nginx以及相关简介

1、安装工具包

  yum install -y wget  下载工具

  yum install -y vim-enhanced  vim编辑器

  yum install -y make cmake gcc gcc-c++  编译源代码

2、安装依赖包

  yum install -y pcre pcre-devel

  yum install -y zlib zlib-devel

  yum install -y openssl openssl-devel

3、下载地址:http://nginx.org/download

  # wget http://nginx.org/download/×××

4、解压压缩包之后进行configure配置

  # ./configure  --perfix=/usr/local/nginx

5、编译安装

  make install

6、启动nginx:# /usr/local/nginx/sbin/nginx

   关闭nginx:#/usr/local/nginx/sbin/nginx -stop 

   重启nginx:#/usr/local/nginx/sbin/nginx -reload

7、开启防火墙80端口

  firewall -cmd --zone=publice --  add-port=80/tcp -- permanent

  firewall -cmd --reload

8、检查nginx安装成功

  ps aux|grep nginx

  网页输入ip查看结果

9、nginx配置 # vi /usr/local/nginx/conf/nginx.con 

  以下为需要配置的代码段

  server {

  listen 80;   #监听端口

  server_name localhost; #监听域名

  #上传文件通常需要设置nginx报文大小限制;避免出现413 请求实体太大

  client_max_body_size 1024M;   

  # 如果没有location更明确的匹配访问路径的话,访问请求都会被该location处理。

  location / {

  #root指定nginx的根目录为/usr/local/nginx/html

  root html;

  # 默认访问文件,欢迎页先去html目录下找index.html,如果找不到再去找index.htm

  index index.html index.htm;}

10、nginx的主要用途

  [1]反向代理

  反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet上 的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 internet 上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

  server {
  listen 80;
  server_name localhost;

  location /gcisp-web/ {
  proxy_pass http://10.153.130.47:5050/gcisp/;
  proxy_connect_timeout 5s;
  #proxy_read_timeout 20s;
  }

  location /gcisp/ {
  proxy_pass http://10.148.16.95:8080/gcisp/;
  proxy_connect_timeout 5s;
  #proxy_read_timeout 20s;}}

  [2] 负载均衡

  负载均衡也是 Nginx 常用的一个功能,负载均衡其意思就是分摊到多个操作单元上进行执行。

  (1)每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。

  upstream server {
  ip_hash;
  server localhost:8080;
  server localhost:8081;}
  server {
  listen 80;
  server_name localhost;

  location / {
  proxy_pass http://test;
  proxy_set_header Host $host:$server_port;}}

  (2)fair(第三方)

  按后端服务器的响应时间来分配请求,响应时间短的优先分配。

  upstream server{
  server localhost:8080;
  server localhost:8081;
  fair;}

  [3]文件映射

  映射服务器的文件资源

  server {
  listen 81;
  server_name localhost; 

  location /contract/{
  alias /home/gcisp/contract/;
  expires 10d;
  autoindex on;
  index index.html index.htm;}}

  [4]HTTP服务器  

  Nginx也是一个静态资源的服务器,当只有静态资源的时候,就可以使用Nginx来做服务器,同时也可以动静分离。

  (1) 静态资源

  server {
  listen 80;
  server_name localhost;

  location / {
  root c:\root;
  index index.html;}}
  # 访问到c盘root目录下面的index.html

  (2) 动静分离

  动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作。

  upstream server{
  server localhost:8080;
  server localhost:8081; }

  server {
  listen 80;
  server_name localhost;

  location / {
  root c:\root;
  index index.html; }

  # 所有静态请求都由nginx处理,存放目录为html
  location ~ \.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
  root c:\root;
  }

  # 所有动态请求都转发给tomcat处理
  location ~ \.(jsp|do)$ {
  proxy_pass http://test;
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
  root c:\root; }}

  [5] 正向代理

  一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。resolver是配置正向代理的DNS服务器,listen 是正向代理的端口

  resolver 114.114.114.114 8.8.8.8;
  server {

  resolver_timeout 5s;

  listen 80;

  location / {
  proxy_pass http://$host$request_uri;}}

11、附录nginx.conf详细配置:https://blog.csdn.net/tjcyjd/article/details/50695922

猜你喜欢

转载自www.cnblogs.com/wu-wu/p/9639504.html
今日推荐