11_Linux环境下安装Nginx

一、查看CentOS的版本

1. cat /etc/redhat-release

二、添加资源库

在 CentOS 系统上安装 Nginx ,得先去添加一个资源库:

vim /etc/yum.repos.d/nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1

三、安装nginx

yum -y install nginx
01.png

四、测试nginx配置文件是否正确

nginx -t

打印如下:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

五、CentOS7.0+ nginx实现停止、启动、重启

systemctl stop  nginx.service   
systemctl start  nginx.service
systemctl restart nginx.service
systemctl status nginx.service

开机自启:
systemctl enable nginx.service
取消开机自启:
systemctl disable nginx.service

六、nginx.conf配置示例

我的配置:

通常我将 upstream服务器的配置写在nginx.conf配置文件中。https及具体路径的转发配置写在包含的default.conf配置文件中.

1. vim /etc/nginx/nginx.conf

    user  nginx;
    worker_processes  2;

    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;

    events {
        worker_connections  2048;
    }

    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        upstream  member-server  {
                  #ip_hash;
                  server  192.168.5.125:80  max_fails=2 fail_timeout=30s ;
        }

        upstream  epc-server  {
                  #ip_hash;
                  server  192.168.5.125:7003  max_fails=2 fail_timeout=30s ;
        }

        upstream  pay-server  {
                  #ip_hash;
                  server  192.168.5.125:7004  max_fails=2 fail_timeout=30s ;
        }

        upstream  door-server  {
                  #ip_hash;
                  server  192.168.5.124:7005  max_fails=2 fail_timeout=30s ;
        }

        upstream  web-server  {
                  #ip_hash;
                  server  192.168.5.124:7006  max_fails=2 fail_timeout=30s ;
        }

        include /etc/nginx/conf.d/*.conf;
    }

2. vim /etc/nginx/config.d/default.conf

    server {
        listen 443;
        listen 80;
        server_name exp.invengo.cn;
        ssl on;
        root html;
        index index.html index.htm;
        ssl_certificate   /etc/nginx/cert/214746778840362.pem;
        ssl_certificate_key  /etc/nginx/cert/214746778840362.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;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        #location / {
        #    root   /usr/share/nginx/html;
        #    index  index.html index.htm;
        #}

        location / {
            root   /usr/local/server/app/web;
            index  index.html index.htm;
        }

        location /box-member {
            proxy_pass http://member-server;
            #root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /box-epc {
            proxy_pass http://epc-server;
            #root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /invengo/pay {
            proxy_pass http://pay-server;
            #root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /invengo/wxpay {
            proxy_pass http://pay-server;
            #root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /box-door {
            proxy_pass http://door-server;
            #root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        location /box-web {
            proxy_pass http://web-server;
            #root   /usr/share/nginx/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   /usr/share/nginx/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;
        #}
    }   

常用的nginx.conf配置:

user  www www;
worker_processes  2;
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  2048;
}
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  65;
  # gzip压缩功能设置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
    gzip_vary on;
  
  # http_proxy 设置
    client_max_body_size   10m;
    client_body_buffer_size   128k;
    proxy_connect_timeout   75;
    proxy_send_timeout   75;
    proxy_read_timeout   75;
    proxy_buffer_size   4k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size   64k;
    proxy_temp_file_write_size  64k;
    proxy_temp_path   /usr/local/nginx/proxy_temp 1 2;
  # 设定负载均衡后台服务器列表 
    upstream  backend  { 
              #ip_hash; 
              server   192.168.10.100:8080 max_fails=2 fail_timeout=30s ;  
              server   192.168.10.101:8080 max_fails=2 fail_timeout=30s ;  
    }
  # 很重要的虚拟主机配置
    server {
        listen       80;
        server_name  itoatest.example.com;
        root   /apps/oaapp;
        charset utf-8;
        access_log  logs/host.access.log  main;
        #对 / 所有做负载均衡+反向代理
        location / {
            root   /apps/oaapp;
            index  index.jsp index.html index.htm;
            proxy_pass        http://backend;  
            proxy_redirect off;
            # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            proxy_set_header  Host  $host;
            proxy_set_header  X-Real-IP  $remote_addr;  
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
            
        }
        #静态文件,nginx自己处理,不去backend请求tomcat
        location  ~* /download/ {  
            root /apps/oa/fs;  
            
        }
        location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$   
        {   
            root /apps/oaapp;   
            expires      7d; 
        }
        location /nginx_status {
            stub_status on;
            access_log off;
            allow 192.168.10.0/24;
            deny all;
        }
        location ~ ^/(WEB-INF)/ {   
            deny all;   
        }
        #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;
        }
    }
  ## 其它虚拟主机,server 指令开始
}

参考:

http://seanlook.com/2015/05/17/nginx-install-and-config/ - nginx服务器安装及配置文件详解

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容