nginx 的基本配置

简介: 安装(mac):关于brew  的问题: http://www.cnblogs.com/adouwt/p/8042201.htmlbrew install nginx 启动:brew services start  nginx 重启:brew services restart  nginx...

安装(mac):关于brew  的问题: http://www.cnblogs.com/adouwt/p/8042201.html

brew install nginx 

启动:

brew services start  nginx 

重启:

brew services restart  nginx 

停止:

brew services stop nginx 

修改配置文件:

安装成功后,会在/etc/nginx 有个 nginx.conf 文件,这里做一些反向代理的操作

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  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;      // 监听端口
      server_name  localhost;  // 本地访问的域名 
      charset utf-8;      // 编码格式
      #access_log  logs/host.access.log  main;
      root   /Users/wangtao/Desktop/my-test-files;  // 访问的根路径  这个是我本地的桌面的某个文件夹的地址
      index  iindex.html iindex.htm;               
     // 默认访问的文件, 我把它改了不是 index.html 主要就是想访问的是这个文件夹,不是某个默认的文件
# 开启目录浏览 autoindex on; autoindex_exact_size off; autoindex_localtime on; #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$ { # 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 # #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; # } #} include vhosts/*.conf; // 这里include 的配置 意思是 同级目录下 vhost 的文件夹下所有 .conf 的配置文件 }

知道这些简单的配置知识,我们就可以进行简单的做一些反向代理了,可以在这个 vhost 下新建我们的配置文件,当然这个文件夹是可以随便取名字的。我们新建一个文件叫myself.conf

server {
  listen 80; // 反向代理的时候,基本都是这个80端口,服务启动的是那个端口就代理哪个端口
  server_name test.itlab.wang;  // 访问的域名 

  location / {
    root /Users/wangtao/it-cloud-lab/www-mobile-client-phone/dist; // 访问的根目录地址,我这里是webpack 打包的一个地址
    index index.html; // 默认访问的 index.html 的文件
  }
}

修改配置文件后,需要重新加载下配置nginx文件,  nginx -s reload   如果这个nginx 服务是root 权限的时候,加sudo 启动  sudo nginx -s reload  ,这时候需要输入计算机密码

这个命令可以按字面意思解释: 保存并重载

 

linux (阿里云服务器):

安装依赖:

 

yum install -y epel-*
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel



安装nginx
yum install nginx
安装好后就是 修改配置文件 。做一些反向代理的操作

下面的就是 将9000端口的服务代理到80端口,通过访问 csa.scampus.cn 就可以看到程序了

server {
  listen 80;
  server_name csa.scampus.cn;
  root /usr/share/nginx/html;
  include /etc/nginx/default.d/*.conf;
  location / {
    proxy_pass http://127.0.0.1:9000;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html{
  }
 }

 

windows 安装

一. 下载

 
下载后解压,如下

 

 

二. 修改配置文件

nginx配置文件在 nginx-1.14.0\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;


events {
    worker_connections  1024;
}


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  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8008;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   E:\vue\vue-dist\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$ {
        #    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;
        server_name  localhost;

        location / {
            root   E:\mystatic_folder;
            index  iindex.html iindex.htm;
            autoindex on;        }
    }


    # HTTPS server
    #
    #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.exe,这样会导致修改配置后重启、停止nginx无效,需要手动关闭任务管理器内的所有nginx进程
 
在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx 
 
start nginx : 启动nginx
nginx -s reload  :修改配置后重新加载生效
nginx -s reopen  :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:
nginx -s stop  :快速停止nginx
nginx -s quit  :完整有序的停止nginx


如果遇到报错:

bash: nginx: command not found

有可能是你再linux命令行环境下运行了windows命令,

如果你之前是允许 nginx -s reload报错, 试下 ./nginx -s reload

或者 用windows系统自带命令行工具运行

目录
相关文章
|
9天前
|
应用服务中间件 Linux nginx
Jetson 环境安装(四):jetson nano配置ffmpeg和nginx(亲测)之编译错误汇总
这篇文章是关于在Jetson Nano上配置FFmpeg和Nginx时遇到的编译错误及其解决方案的汇总。
46 4
|
5天前
|
缓存 负载均衡 安全
Nginx常用基本配置总结:从入门到实战的全方位指南
Nginx常用基本配置总结:从入门到实战的全方位指南
112 0
|
10天前
|
编解码 Ubuntu 应用服务中间件
Jetson 环境安装(三):jetson nano配置ffmpeg和nginx(亲测)
本文介绍了在NVIDIA Jetson Nano上配置FFmpeg和Nginx的步骤,包括安装、配置和自启动设置。
69 1
Jetson 环境安装(三):jetson nano配置ffmpeg和nginx(亲测)
|
4天前
|
前端开发 JavaScript 应用服务中间件
终极 Nginx 配置指南
本文介绍了Nginx的基本配置及其优化方法。首先,通过删除注释简化了Nginx的默认配置文件,使其更易于理解。接着,文章将Nginx配置文件分为全局块、events块和http块三部分进行详细解释。此外,还提供了如何快速上线网站、解决前端history模式404问题、配置反向代理、开启gzip压缩、设置维护页面、在同一IP上部署多个网站以及实现动静分离的具体配置示例。最后,附上了Nginx的基础命令,包括安装、启动、重启和关闭等操作。
|
8天前
|
负载均衡 应用服务中间件 nginx
Nginx的6大负载均衡策略及权重轮询手写配置
【10月更文挑战第9天】 Nginx是一款高性能的HTTP服务器和反向代理服务器,它在处理大量并发请求时表现出色。Nginx的负载均衡功能可以将请求分发到多个服务器,提高网站的吞吐量和可靠性。以下是Nginx支持的6大负载均衡策略:
33 7
|
10天前
|
缓存 前端开发 应用服务中间件
CORS跨域+Nginx配置、Apache配置
CORS跨域+Nginx配置、Apache配置
49 7
|
6天前
|
缓存 前端开发 JavaScript
一、nginx配置
一、nginx配置
54 1
|
15天前
|
存储 缓存 监控
|
16天前
|
安全 应用服务中间件 Shell
nginx配置https的ssl证书和域名
nginx配置https的ssl证书和域名
|
16天前
|
存储 缓存 前端开发
理清 nginx 中的 location 配置
理清 nginx 中的 location 配置