栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

nginx安装及配置 踩坑指南

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nginx安装及配置 踩坑指南

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

nginx的安装和配置
  • 一.安装步骤
    • 1.官网下载好并上传到服务器 (文件夹可以自定义,随便哪个都可以,以demo为例)
    • 2.解压下载好的压缩包
    • 3.配置基本信息
    • 4. 编译安装
    • 5.进入到之前配置的安装目录下,启动命令
    • 6.去浏览器打开ip可以查看到nginx示范页面
  • 二,配置
    • conf中的nginx.conf里面配置
  • 三.nginx相关命令


一.安装步骤 1.官网下载好并上传到服务器 (文件夹可以自定义,随便哪个都可以,以demo为例)

官网地址:http://nginx.org/en/download.html
下载后将包放到demo文件夹
注释:根据当前服务器系统来下载匹配的版本(一般为Linux)

2.解压下载好的压缩包

命令如下:

# 解压到当前目录下
cd /demo
tar -zxvf 资源包

3.配置基本信息

命令如下:

//配置configure --prefix 代表安装的路径,--with-http_ssl_module 安装ssl,--with-http_stub_status_module查看nginx的客户端状态
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

(/usr/local/nginx)这个为安装地址,可以自行设置

4. 编译安装
#编译安装nginx
cd /demo/nginx
make & make install 
5.进入到之前配置的安装目录下,启动命令
cd /usr/local/nginx/sbin
./nginx  // 启动命令

6.去浏览器打开ip可以查看到nginx示范页面 二,配置 conf中的nginx.conf里面配置

路径: /usr/local/nginx/conf/nginx.conf

#user  nobody;  // 启动时用户身份
worker_processes  1;    //  工作时进程数(*正常情况下有一个主进程和最少一个工作进程*)

#error_log  logs/error.log; // 全局错误日志存放地址
#error_log  logs/error.log  notice;  // 全局错误日志格式
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;  // pid存放地址


events {
    worker_connections  1024; // 工作进程最大并发数
}


http {
    include       mime.types;    // 定义web支持的文件类型
    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  0;
    keepalive_timeout  65;  // 长链接的超时限制

    #gzip  on;  // 是否开启压缩

    server { // 服务配置
        listen       80;  // 监视端口
        server_name  localhost;  // 服务名称

        #charset koi8-r;  // 支持的字符集

        #access_log  logs/host.access.log  main; // 访问日志

        location / {
            root   /opt/nginx/dist;  // 定义前端代码存放地址
            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   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$ {  // 配置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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    # // 配置多个服务
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    # // https 相关配置
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

三.nginx相关命令
ip addr  // 查看当前服务本地ip
./nginx -s reload  // 重启nginx 
ps -ef | grep nginx // 查看当前nginx所有进程
./nginx // 启动服务
pkill -9 nginx   // 关闭所有nginx进程
lsof -i :80 // 查看占用80端口的进程
kill -9 1 // 关闭端口中pid为1的进程
netstat -ntlp //  查看所有端口
fuser -k 80/tcp // 关闭所端口为80的进程





转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/840965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号