阿里云Centos服务器安装Nginx

  • 安装依赖

    yum install openssl

    yum install zlib

    yum install pcre

    rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
  • 安装Nginx

    yum install nginx
  • 启动Nginx/重启Nginx

    service nginx start  # 启动
    service nginx restart  # 重启
  • 验证安装

    在安全组设置中,开放服务器80端口。
    浏览器访问http://{服务器ip},出现欢迎界面表示安装且启动成功。

    # 配置文件路径
    /etc/nginx/nginx.conf
    # 上面的文件默认会引用/etc/nginx/conf.d文件夹下的所有结尾为.conf的配置文件,其中该文件夹默认存在一个default.conf配置文件。
  • 简单使用

    • 转发

      server {
        listen       80;
        server_name localhost;

         #access_log /var/log/nginx/host.access.log main;

        location / {
             # root   /usr/share/nginx/html;
             # index index.html index.htm;
            proxy_pass http://127.0.0.1:5000;  # 将服务端80端口接收的请求转发到127.0.0.1:5000
        }

         #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;
         #}
      }
    • 负载均衡

      upstream myserver {
            ip_hash;  # 负载均衡算法,有多种可选,可以选择多种均衡策略
            server 127.0.0.1:5000;
            server 127.0.0.1:5001;
            server 127.0.0.1:5002;
        }


      server {
        listen       80;
        server_name localhost;

         #access_log /var/log/nginx/host.access.log main;

        location / {
            proxy_pass http://myserver;  # 将服务器80端口接收的请求转发到负载均衡组myserver中
             # 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;
         #}
      }
posted on 2024-03-25 17:10  CJTARRR  阅读(91)  评论(0编辑  收藏  举报