云服务器网:购买云服务器和VPS必上的网站!

nginx安装使用(nginx安装与使用)

本文目录:1、如何安装nginx2、使用docker容器安装nginx的使用3、mac上安装Nginx详细教程如何安装nginx1)下载地址:2)启动解压至c:\\nginx,运行nginx.exe(即nginx -c conf\\nginx.conf),默认使用80端口,日志见文件夹C:\\nginx\\

本文目录:

  • 1、如何安装nginx
  • 2、使用docker容器安装nginx的使用
  • 3、mac上安装Nginx详细教程

如何安装nginx

1)下载地址:

2)启动

解压至c:\nginx,运行nginx.exe(即nginx -c conf\nginx.conf),默认使用80端口,日志见文件夹C:\nginx\logs

3)使用

4)关闭

nginx -s stop 或taskkill /F /IM nginx.exe nul

5)常用配置

C:\nginx\conf\nginx.conf,使用自己定义的conf文件如my.conf,命令为nginx -c conf\my.conf

常用配置如下:

Nginx.conf代码

http {

server {

1.侦听80端口

listen 80;

location / {

2. 默认主页目录在nginx安装目录的html子目录。

root html;

index index.html index.htm;

3. 没有索引页时,罗列文件和子目录

autoindex on;

autoindex_exact_size on;

autoindex_localtime on;

}

4.指定虚拟目录

location /tshirt {

alias D:\programs\Apache2\htdocs\tshirt;

index index.html index.htm;

}

}

5.虚拟主机配置

server {

listen 80;

server_name ;

access_log emb.info/logs/access.log;

location / {

index index.html;

root emb.info/htdocs;

}

}

}

http {

server {

1.侦听80端口

listen 80;

location / {

2. 默认主页目录在nginx安装目录的html子目录。

root html;

index index.html index.htm;

3. 没有索引页时,罗列文件和子目录

autoindex on;

autoindex_exact_size on;

autoindex_localtime on;

}

4.指定虚拟目录

location /tshirt {

alias D:\programs\Apache2\htdocs\tshirt;

index index.html index.htm;

}

}

5.虚拟主机配置

server {

listen 80;

server_name ;

access_log emb.info/logs/access.log;

location / {

index index.html;

root emb.info/htdocs;

}

}

}

小提示:

运行nginx -V可以查看该Win32平台编译版支持哪些模块。我这里的结果为:

Log代码

nginx version: nginx/0.7.65

TLS SNI support enabled

configure arguments:

–builddir=objs.msvc8

–crossbuild=win32

–with-debug –prefix=

–conf-path=conf/nginx.conf

–pid-path=logs/nginx.pid

–http-log-path=logs/access.log

–error-log-path=logs/error.log

–sbin-path=nginx.exe

–http-client-body-temp-path=temp/client_body_temp

–http-proxy-temp-path=temp/proxy_temp

–http-fastcgi-temp-path=temp/fastcgi_temp

–with-cc-opt=-DFD_SETSIZE=1024

–with-pcre=objs.msvc8/lib/pcre-7.9

–with-openssl=objs.msvc8/lib/openssl-0.9.8k

–with-openssl-opt=enable-tlsext

–with-zlib=objs.msvc8/lib/zlib-1.2.3

–with-select_module

–with-http_ssl_module

–with-http_realip_module

–with-http_addition_module

–with-http_sub_module

–with-http_dav_module

–with-http_stub_status_module

–with-http_flv_module

–with-http_gzip_static_module

–with-http_random_index_module

–with-http_secure_link_module

–with-mail

–with-mail_ssl_module

–with-ipv6

nginx version: nginx/0.7.65

TLS SNI support enabled

configure arguments:

–builddir=objs.msvc8

–crossbuild=win32

–with-debug –prefix=

–conf-path=conf/nginx.conf

–pid-path=logs/nginx.pid

–http-log-path=logs/access.log

–error-log-path=logs/error.log

–sbin-path=nginx.exe

–http-client-body-temp-path=temp/client_body_temp

–http-proxy-temp-path=temp/proxy_temp

–http-fastcgi-temp-path=temp/fastcgi_temp

–with-cc-opt=-DFD_SETSIZE=1024

–with-pcre=objs.msvc8/lib/pcre-7.9

–with-openssl=objs.msvc8/lib/openssl-0.9.8k

–with-openssl-opt=enable-tlsext

–with-zlib=objs.msvc8/lib/zlib-1.2.3

–with-select_module

–with-http_ssl_module

–with-http_realip_module

–with-http_addition_module

–with-http_sub_module

–with-http_dav_module

–with-http_stub_status_module

–with-http_flv_module

–with-http_gzip_static_module

–with-http_random_index_module

–with-http_secure_link_module

–with-mail

–with-mail_ssl_module

–with-ipv6

显然,最经常用的memcache, rewrite模块都没在其中,因此该win32编译版本仅能供基本开发测试使用,对于产品平台,应该重新编译自己想要的win32版本,或者在linux下使用更方便。

6)查看nginx进程

tasklist /fi “imagename eq nginx.exe”,如下显示:

映像名称 PID 会话名 会话 内存使用

========================= ======== ================ =========== ============

nginx.exe 8944 Console 1 5,128 K

nginx.exe 6712 Console 1 5,556 K

7)nginx常用命令

nginx -s stop 强制关闭

nginx -s quit 安全关闭

nginx -s reload 改变配置文件的时候,重启nginx工作进程,来时配置文件生效

nginx -s reopen 打开日志文件

8)其它

可以通过配置文件开启多个nginx工作进程,但同时只有其中一个nginx工作进程在工作,其他的阻塞等待。

一个nginx工作进程最多同时可以处理1024个连接。

nginx中需要共享内存的cache或者模块无法在windows下正常使用。

不过,nginx官方正在改进,将来nginx会以服务的方式运行,使用 I/O completion ports代替select方法,使多个工作进程能并发工作。

要使用nginx配合php-cgi使用,需要修改环境变量,否则,php-cgi运行一定次数就推出,需要重启,设置PHP_FCGI_MAX_REQUESTS这个变量为0即可。

以上在win7上通过。

8)nginx以windows服务形式启动

1.下载微软两个工具:

instsrv.exe srvay.exe

2.执行命令:

instsrv Nginxc:/nginx/srvany.exe

3.配置Nginx的运行参数

可以直接将配置导入到注册表

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/NGINX/Parameters]

“Application”=”C://nginx//nginx.exe”

“AppParameters”=””

“AppDirectory”=”C://nginx//”

注意:windows 下的Nginx 内置的module 很多没有,用Nginx -V 命令查看。

9)Nginx下部署mono+asp.net环境

1、从Mono for Windows中提取FastCGI-Mono-Server

2、Nginx nginx.conf 的配置:

worker_processes 1;

error_log logs/error-debug.log info;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type text/plain;

sendfile on;

keepalive_timeout 65;

index index.html index.htm;

server {

listen 80;

server_name yourdomain.com;

index index.aspx default.aspx;

location / {

root D:\www/yourwebapp;

fastcgi_pass 127.0.0.1:8000;

fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;

include fastcgi_params;

}

}

}

将上面的 FastCGI-Mono-Server 提取出来,所有文件全部注册到 GAC(否则 Web 应用会找不到他们,当然你也可以直接放到 webapp/bin),然后解压到某个文件夹,这里假设为 D:/FastCGI-Mono-Server。

之后我们就可以按下列命令运行 FastCGI:

fastcgi-mono-server2 /socket=tcp:127.0.0.1:8000 /root=”D:\www\yourwebapp” /applications=yourdomain.com:/:. /multiplex=True

最后执行运行 Nginx 服务器,我们的 ASP.Net 程序就能脱离 IIS。

使用docker容器安装nginx的使用

按我的理解就是linux上面的一个虚拟机,和window一样也有虚拟机。能做的事情很多,这里简单讲下nginx在docker上面的使用。

真实场景下:简而言之,假设在A服务器上面安装过基础环境,并且有了配置啥的,可以把安装过的基础环境用docker打成包,下次B服务器可以直接用docker安装这个包,用在去重复配置基础环境和其他配置啥的,相当于可以复用。

我这里因为客户要求不能用root帐户权限使用nginx,我的nginx是用rpm包安装的(rpm包安装很简单,但是没有root权限时候就不能使用rpm包安装nginx),加上是内网环境(内网环境安装nginx很麻烦!要依赖各种以及gcc编译安装等等),所以最终采用的是docker相对简单,将docker安装上,再安装nginx镜像。

访问原理是将Linux的端口映射到docker里的nginx配置开启的端口,和直接nginx做服务容器没区别。

通过镜像包安装:(安装方式一)

docker load -i nginx.tar 安装nginx镜像包

docker tag 6084105296a9 nginx:latest  镜像命名

docker run –name nginx -p 9000:80 -d nginx  生成新容器(镜像模式)

通过容器包安装:(安装方式二)

docker import nginx.tar nginx  安装nginx容器包

生成新容器(容器安装模式):

docker run –name nginx -d -p 9001:80 nginx  /docker-entrypoint.sh nginx -g ‘daemon off;’

其他常用命令:

docker images  查看镜像

docker ps  查看容器、容器id

docker rmi nginx 删除镜像

docker rm nginx 删除容器

docker stop nginx  停止容器

docker start nginx

docker restart  nginx

docker exec -it nginx /bin/bash  进入容器

exit  退出容器

docker cp default.conf nginx:/etc/nginx/conf.d/default.conf    拷贝nginx的配置到docker的nginx里面

docker cp dist nginx:/usr/share/nginx/html     拷贝前端dist到docker的nginx里面

docker打成镜像包命令:

docker commit 容器id 镜像名称:版本号

docker save -o 镜像包名称.tar 镜像名称

(备注:1、版本号可以不写,2、打成镜像包后会在当前的目录可以看到)

假设已经安装好了docker,配置好了nginx,现在前端修i改了页面,重新打包dist,只需要把dist文件夹上传到linux上面,然后拷贝到docker里面就可以,不需要重启docker,也不需要重启nginx。

1、将打包好的文件上传到 : /etc/nginx/html

2、把文件拷贝到docker目录下:docker cp dist nginx:/etc/nginx/html

mac上安装Nginx详细教程

mac上安装Nginx详细教程

1. 安装(可以用 brew 安装)

sudo brew install nginx

2. 查看 nginx 版本

nginx -v

3. 启动 nginx

sudo nginx

也可以使用下面的命令启动,但是配置文件nginx.conf修改后用这个命令执行不生效,故不建议使用:

sudo brew services start nginx

image

4. 查看 nginx 是否启动成功

在浏览器中访问 ,如果出现如下界面,则说明启动成功.

image

备注:端口号是在配置文件 nginx.conf 里面配置的,默认端口是 8080 ,配置文件的位置 /usr/local/etc/nginx

5. 关闭nginx

sudo nginx -s stop

也可以使用下面的命令启动,但是配置文件nginx.conf修改后用这个命令执行不生效,故不建议使用:

sudo brew services stop nginx

6. 重新加载nginx

sudo nginx -s reload

7. 可能遇到的问题

端口被占用

nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)

解决方法:修改 nginx.conf 文件里的端口号

权限不够

nginx: [alert] could not open error log file: open() “/usr/local/var/log/nginx/error.log” failed (13: Permission denied)

解决方法:在命令前加上 sudo,这时可能会要求输入密码,密码就是电脑的开机密码啦~

cd /usr/local/cellar/nginx/1.12.1/bin➜ bin sudo brew services start nginxbin chmod a+x ./nginxchmod: Unable to change file mode on ./nginx: Operation not permitted//将nginx文件添加权限➜  bin sudo chmod a+x ./nginx➜  bin sudo ./nginxnginx: [emerg] getgrnam(“root”) failed in /usr/local/etc/nginx/nginx.conf:2//修改nginx.conf文件,然后重新启动nginx➜  1.12.1 cd bin➜  bin sudo ./nginx➜  bin sudo nginx -s reload➜  bin sudo nginx -s stop

8. 补充

安装 homebrew ,将以上命令粘贴至terminal,然后回车即可

/usr/bin/ruby -e “$(curl -fsSL )”

常用的指令有:

nginx-s reload 重新加载配置nginx-s reopen 重启nginx-s stop 停止nginx-s quit 退出nginx-V 查看版本,以及配置文件地址nginx-v 查看版本nginx-c filename 指定配置文件nginx-h 帮助

本文来源:https://www.yuntue.com/post/156956.html | 云服务器网,转载请注明出处!

关于作者: yuntue

云服务器(www.yuntue.com)是一家专门做阿里云服务器代金券、腾讯云服务器优惠券的网站,这里你可以找到阿里云服务器腾讯云服务器等国内主流云服务器优惠价格,以及海外云服务器、vps主机等优惠信息,我们会为你提供性价比最高的云服务器和域名、数据库、CDN、免费邮箱等企业常用互联网资源。

为您推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注