Linux——Nginx 安装

系统 centOS6.5

下载 nginx

安装依赖包

  • yum install -y pcre pcre-devel

  • yum install -y zlib zlib-devel yum

  • yum install -y openssl openssl-devel

解压安装包

  • tar -zxvf nginx-1.15.1.tar.gz

  • cd nginx-1.15.1

配置 nginx

  • ./configure --prefix=/usr/local/nginx

  • 需要注意的是如果配置的是 https 协议,那么上面的配置是不行的,如下配置一步到位

  • ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

  • nginx 重要文件存放路径

    nginx path prefix: "/user/local/nginx"
    nginx binary file: "/user/local/nginx/sbin/nginx"
    nginx modules path: "/user/local/nginx/modules"
    nginx configuration prefix: "/user/local/nginx/conf"
    nginx configuration file: "/user/local/nginx/conf/nginx.conf"
    nginx pid file: "/user/local/nginx/logs/nginx.pid"
    nginx error log file: "/user/local/nginx/logs/error.log"
    nginx http access log file: "/user/local/nginx/logs/access.log"
    nginx http client request body temporary files: "client_body_temp"
    nginx http proxy temporary files: "proxy_temp"
    nginx http fastcgi temporary files: "fastcgi_temp"
    nginx http uwsgi temporary files: "uwsgi_temp"
    nginx http scgi temporary files: "scgi_temp"
    

安装 nginx

  • make

  • make install

启动 nginx

  • service httpd stop,apache 有可能占据了80端口

  • /usr/local/nginx/sbin/nginx

特别补充

  • 重载配置文件

    • /usr/local/nginx/sbin/nginx -s reload
  • 卸载编译安装的软件

    • 先停止服务,killall -9 nginx

    • rm -rf 软件安装目录

  • 强制使用 https 访问

    server {  
        listen  192.168.1.111:80;  
        server_name test.com;     
        rewrite ^(.*)$  https://$host$1 permanent;  
    }  
    
  • 外部接口 https,内部反向代理转 http

    server {
        listen      443;
        server_name  www.test.com;
    
        ssl on;
        ssl_certificate   cert/2142977640231.pem;
        ssl_certificate_key  cert/2142977640231.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 / {
            proxy_pass http://192.168.97.5:9888;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection keep-alive;
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto  $scheme;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
  • 基本配置

    server {
        listen       443 ssl;
        server_name  cnoop.link;
    
        ssl_certificate      cert/1531777324520.pem;
        ssl_certificate_key  cert/1531777324520.key;
    
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
    
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
    
        location / {
            root   html/cnloop;
            index  index.html index.htm;
        }
    
        location /api {
            rewrite /api/(.*) /$1  break;
            proxy_pass         http://127.0.0.1:3000;
            proxy_redirect     off;
            proxy_set_header   Host $host;
        }
    
        error_page  404 403  /200.html;
    }
    
  • 安装帮助

原文地址:https://www.cnblogs.com/cnloop/p/9276985.html