Linux安装Nginx

Centos7安装Nginx的步骤

Linux安装Nginx

1.准备目录

mkdir -p  /data/nginx
cd /data/nginx

2.下载

cd /data/nginx
    
-- 安装GCC
 yum -y install gcc gcc-c++ autoconf automake make
    
从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1.5.9.tar.gz直接在Linux上用命令下载)

tar -zxvf nginx-1.5.9.tar.gz

3.nginx安装

①设置Nginx的安装路径:

cd /data/nginx/nginx-1.5.9

./configure --prefix=/data/nginx/nginx-1.5.9

可能会出现这个错误

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

如果出现了,就执行下这个

 yum -y install openssl openssl-devel

②编译:

make (make的过程是把各种语言写的源码文件,变成可执行文件和各种库文件)

// make install  (可以不执行 make install是把这些编译出来的可执行文件和库文件复制到合适的地方)

③启动:

参数 -c 指定了配置文件的路径,如果不加的话就是使用默认的配置文件

/data/nginx/nginx-1.5.9/sbin/nginx  -c  /data/nginx/nginx-1.5.9/conf/nginx.conf

注意: 如果现实没有日志文件 需要手动创建相应的日志文件

测试:
/data/nginx/nginx-1.5.9/sbin/nginx  -t
重启:
/data/nginx/nginx-1.5.9/sbin/nginx -s reload

4.配置请求转发

以下为无ssl证书配置的请求转发

server {
    listen 80;
    server_name api.****.com;
    #以下为指定请求域名匹配到某一个端口
    #location ~* /union {
    #以下为全部转发到某一个端口
    location / {
      client_max_body_size   3000m;
      proxy_next_upstream http_502 http_504 error timeout invalid_header;

      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Real-PORT $remote_port;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://127.0.0.1:8080;
      proxy_redirect default;
      proxy_connect_timeout 3000;

      if ($http_origin ~ https://(.*).****.com){
          set $allow_url $http_origin;
      }

      if ($request_method = OPTIONS) {
          add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS, HEAD";
#         add_header Access-Control-Allow-Origin $allow_url;
          add_header Access-Control-Allow-Origin *;
          add_header Access-Control-Allow-Headers "Cookie,Set-Cookie,Origin,X-Requested-With,token,authorization,Accept,Content-type";
          add_header Access-Control-Allow-Credentials "true";
          add_header Content-Type "text/plain; charset=utf-8";
          add_header Content-Length 0;
        return 204;
      }
    }
}


以下为有ssl请求转发


server {
    listen 80;
    server_name api.****.com;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}

server {
    listen 443;
    server_name api.****.com;
    ssl on;
    ssl_certificate   /etc/nginx/cert/api.pem;
    ssl_certificate_key  /etc/nginx/cert/api.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    #以下为指定请求域名匹配到某一个端口
    #location ~* /union {
    #以下为全部转发到某一个端口
    location / {
      client_max_body_size   3000m;
      proxy_next_upstream http_502 http_504 error timeout invalid_header;

      proxy_set_header   Host $host;
      proxy_set_header   X-Real-IP $remote_addr;
      proxy_set_header   X-Real-PORT $remote_port;
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://127.0.0.1:8080;
      proxy_redirect default;
      proxy_connect_timeout 3000;

      if ($request_method = OPTIONS) {
          add_header Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS, HEAD";
          add_header Access-Control-Allow-Origin *;
          add_header Access-Control-Allow-Headers "Cookie,Set-Cookie,Origin,X-Requested-With,token,authorization,Accept,Content-type";
          add_header Access-Control-Allow-Credentials "true";
          add_header Content-Type "text/plain; charset=utf-8";
          add_header Content-Length 0;
        return 204;
      }
    }
}

5.其他:

   upstream tserver { 
      server 10.23.57.227:9001; #localhost也可以  注意10.23.57.227 为电脑连接外网的ip地址
      jvm_route $cookie_JSESSIONID; # jvm相关
      #server 10.23.57.227:8080/aaa/index.html/
}

 
  server {
    listen       80;
    erver_name  xiha.com;
     location / {
       index  index.html index.htm;
           proxy_pass  http://tserver;#代理,转发到集群;
        }
  }
静态图片配置:
server {
                listen 80;
                server_name localhost;
 
                #charset koi8-r;
 
                #access_log logs/host.access.log main;
 
                location ~ .*\.(gif|jpg|jpeg|png)$ {
                        expires 24h;
                       root /usr/local/nginx/html/images;#指定图片存放路径
                       access_log /data/soft/nginx/logs/images.log;#日志存放路径
                       proxy_store on;
                       proxy_store_access user:rw group:rw all:rw;
                       proxy_temp_path /usr/local/nginx/html/images;#图片访问路径
                       proxy_redirect off;
                       proxy_set_header Host 127.0.0.1;
                       client_max_body_size 10m;
                       client_body_buffer_size 1280k;
                       proxy_connect_timeout 900;
                       proxy_send_timeout 900;
                       proxy_read_timeout 900;
                       proxy_buffer_size 40k;
                       proxy_buffers 40 320k;
                       proxy_busy_buffers_size 640k;
                       proxy_temp_file_write_size 640k;
                       if ( !-e $request_filename)
                       {
                                 proxy_pass http://127.0.0.1;#默认80端口
                       }
         }
 
                      location / {
                      root /usr/local/nginx/html/images; #html访问路径
                      index index.html index2.htm; #html文件名称
                      }
          }
css等配置:
  location /resources {
            alias /data/webhtml/node-server/resources/;
            autoindex on;
          
        }
指定路径的首页:
location / {
            rewrite ^/(.*) /index.html last;
        }
        

        location ~ \.(html|ico|txt)$ {
            # html不缓存
            add_header Cache-Control no-store;
            root /data/ucai_project/webhtml/node-server;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值