Windows下Nginx的安装及配置

一、Nginx简介

1.Nginx是什么

Nginx是一款轻量级Web服务器、也是一款反向代理服务器

2.Nginx能干什么

①可直接支持Rails和PHP的程序
②可作为HTTP反向代理服务器
③作为负载均衡服务器
④作为邮件代理服务器
⑤帮助实现前端动静分离

3.Nginx特点

  • 高稳定
  • 高性能
  • 资源占用少
  • 功能丰富
  • 模块化结构
  • 支持热部署
二、Nginx安装

1.下载:http://nginx.org/download/nginx-1.10.2.zip

2.解压缩

3.运行nginx.exe:通过双击图标或者cmd命令行运行

三、Nginx常用命令

1.测试配置文件

安装路径下的 nginx.exe -t

2.启动命令

安装路径下的 nginx.exe

3.停止命令

安装路径下的 nginx.exe -s stop,
或者是:nginx.exe -s quit

4.重启命令

安装路径下的 nginx.exe -s reload

5.查看进程命令

ps -ef |grep nginx

6.平滑重启

kill -HUP 【Nginx主进程号(即查看进程命令查到的PID)】

7.增加防火墙访问权限

①sudo vim /etc/sysconfig/iptables
②-A INPUT -p tcp -m state –state NEW
-m tcp –dport 80 -j ACCEPT
③保存退出
④重启防火墙 sudo service iptables restart

四、Nginx虚拟域名配置及测试验证

配置步骤:

1.编辑sudo vim /usr/local/nginx/conf/nginx.conf
①增加include vhost/*.conf;
②保存退出

nginx.conf.jpg

2.在/usr/local/nginx/conf/目录新建vhost文件夹
即:/user/local/nginx/conf/vhost

3.创建域名转发配置文件

image.hcxjingdong.com.conf:转向目录的反向代理:
server {
    listen 80;
    autoindex off;
    server_name image.hcxjingdong.com;
    access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
    404. html;
    if ($query_string~ * ".*[\;'\<\>].*") {
        return 404;
    }
    location~/(mmall_fe|mmall_admin_fe)/dist / view /* { 
        deny all; 
    } 
    location / { 
        root C:\ftpfile\img; 
        add_header Access-Control-Allow-Origin *; 
        } 
    }


tomcat.hcxjingdong.com.conf:转向端口的反向代理:
server {
    listen 80;
    autoindex on;
    server_name tomcat.hcxjingdong.com;
    access_log c: /access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /
    404. html;
    if ($query_string~ * ".*[\;'\<\>].*") {
        return 404;
    }
    location / {
            proxy_pass http: //127.0.0.1:8080; 
            add_header Access-Control-Allow-Origin *; 
            } 
    }

4.启动(重启)验证
①启动: nginx/sbin/nginx n g i n x / s b i n / n g i n x ② 重 启 : {nginx}/sbin/nginx -s reload

注:${nginx}代表安装在系统中的路径,例如:/usr/local/nginx

5.访问验证

使用默认80端口访问验证:http://localhost:80http://127.0.0.1:80

6.指向端口

http转发

server{
    listen 80;
    autoindex off;
    server_name learning.hcxjingdong.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:81/learning;
        add_header Access-Control-Allow-Origin *;
    }
}

listen 80:监听80端口;
autoindex off:是否创建首页的索引目录;
当nginx接到image.hcxjingdong.com(二级域名)请求,就转发到:http://127.0.0.1:81/learning目录下

7.指向目录

线上图片服务器,为前端提供的前端部署服务器都是通过指向目录的反向代理

server{
    listen 80;
    autoindex off;
    server_name img.hcxjingdong.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #root /product/front/;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }
    location ~ /(hcxjingdong_fe|hcxmall_admin_fe)/dist/view/* {
        deny all;
    }
    location / {
        root \product\ftpfile\img;
        add_header Access-Control-Allow-Origin *;
    }
}

root /product/ftpfile/img:
root直接指向硬盘系统目录product文件夹下的ftpfile下的img文件夹;
即在访问img.hcxjingdong.com的时候就直接指向了该文件夹

8.测试验证

验证成功页面.jpg

五、Nginx注意事项

可以配置域名转发,但请一定要配置host,并且使host生效之后才可以,设置完成之后要重启浏览器

Windows下配置:
①进入c:\Windows\System32\drivers\etc
②用记事本打开hosts文件
③添加好对应的域名及ip
④保存退出

例如:
10.211.55.6 image.hcx.com
10.211.55.6 s.hcx.com

添加域名及ip.jpg

六、配置Windows下的Nginx

配置hosts:
c:\Windows\System32\drivers\etc

配置hosts.jpg

用浏览器访问www.hcxjingdong.com

验证hosts配置.jpg

包括本机访问http://localhost

本机访问.jpg

配置目录的转发

1.进入到nginx.conf(nginx的主配置):
添加:include vhost/*.conf;

修改nginx.conf.jpg

2.按照该路径去创建此文件夹:
在conf文件夹下创建vhost文件夹

创建vhost文件夹.jpg

3.在vhost文件夹中创建文件:image.hcxjingdong.com.conf

创建image.hcxjingdong.com.conf.jpg

文件内容:

server{
    listen 80;
    autoindex off;
    server_name image.hcxjingdong.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }

    location ~ /(hcxmall_fe|hcxmall_admin_fe)/dist/view/* {
        deny all;
    }

    location / {
        root C:\ftpfile\img;
        add_header Access-Control-Allow-Origin *;
    }
}

到C:\ftpfile\img目录下存放图片以便访问

4.修改本机的host,让本机的nginx配合到image.hcxjingdong.com域名

去到C:\Windows\System32\drivers\etc目录下修改hosts文件:

修改本机的host.jpg

5.重启nginx:

进入到nginx目录执行命令:
①nginx.exe -t:验证配置文件是否正确
②nginx.exe -s reload:重启nginx

验证并重启nginx.jpg

6.访问域名(image.hcxjingdong.com)验证图片是否生效:

测试host是否生效:image.hcxjingdong.com
测试图片是否生效:http://image.hcxjingdong.com/hcx.jpg

验证是否生效.jpg

配置ip端口的转发

1.在conf下的vhost下创建:tomcat.hcxjingdong.com.conf

创建tomcat.hcxjingdong.com.conf.jpg

使用tomcat域名进行ip端口转发,转发到tomcat服务上

tomcat.hcxjingdong.com.conf:

server{
    listen 80;
    autoindex off;
    server_name tomcat.hcxjingdong.com;
    access_log c:/access.log combined;
    index index.html index.htm index.jsp index.php;
    #error_page 404 /404.html;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
    }
    location / {
        proxy_pass http://127.0.0.1:8080;
        add_header Access-Control-Allow-Origin *;
    }
}

2.配置hosts:

配置hosts2.jpg

3.启动tomcat

4.重启nginx:nginx.exe -s reload

5.访问http://tomcat.hcxjingdong.com
成功显示tomcat启动页,说明http的转发也成功了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Windows安装 nginx 比较简单,可以直接从官网下载安装包并进行安装。 1. 从 nginx 官网下载 Windows 版本的安装包(http://nginx.org/en/download.html) 2. 双击安装包并依次按照安装向导的提示进行安装。 3. 安装完成后,在命令行中输入 nginx,如果能看到“nginx 已成功启动”的提示,说明 nginx 安装成功。 配置 nginx: 1. 打开 nginx 安装目录下的 conf/nginx.conf 文件。 2. 修改配置文件中的相关配置,如服务器的地址、端口号等。 3. 保存配置文件。 4. 在命令行中输入 nginx -s reload,重新加载配置文件。 注意:具体配置内容因个人需求而异。 ### 回答2: WindowsNginx安装配置相对简单。下面是详细步骤: 1. 下载Nginx Windows版本的安装包,推荐到官网下载,地址为:http://nginx.org/en/download.html 2. 解压安装包,将解压后的文件夹重命名为nginx,并将其移动到C盘根目录。因为nginx默认会在C盘根目录寻找配置文件。 3. 进入nginx文件夹,打开conf目录下的nginx.conf文件,在开头添加如下代码,指定Nginx的运行目录: ```bash chdir c:/nginx/ ``` 4. 在nginx.conf文件中修改server块的配置,添加监听端口号和root目录,如下: ```bash server{ listen 80; server_name localhost; root html; index index.html index.htm; } ``` 这里的端口号可以根据实际需求修改,root目录则为Nginx的默认站点目录。 5. 启动nginx,在cmd命令行下输入: ```bash cd C:\nginx start nginx ``` 如果没有错误提示,则说明启动成功。 6. 在浏览器中输入localhost,如果看到Nginx的欢迎页面,则表示配置成功。 7. 关闭nginx,在cmd命令栏下输入: ```bash nginx -s stop ``` 注意:如果修改配置文件后需要重新启动Nginx,则输入: ```bash nginx -s reload ``` 总结:以上是WindowsNginx安装配置步骤,如果需要添加虚拟主机、负载均衡等功能,在nginx.conf文件中进行对应的配置即可。 ### 回答3: Nginx是一个高性能的HTTP和反向代理服务器。在Windows操作系统中,通过安装Nginx可以实现Web服务器的搭建以及各种代理和负载平衡操作。下面是WindowsNginx安装配置步骤。 一、下载Nginx 访问Nginx的官网,选择Windows版本的Nginx进行下载,下载后将文件解压到指定目录。 二、配置Nginx 1、进入到Nginx的目录中找到conf文件夹,这个文件夹中包含了Nginx配置文件。 2、打开nginx.conf文件,修改以下选项: 修改listen:将listen 80改为listen 8080,如果端口冲突,可以选择其他端口。 修改server_name:将server_name localhost改为server_name 你的域名,也可以使用IP地址。 修改root:将root html改为你的网站根目录。 3、将文件保存后,关闭文件。 三、启动Nginx服务 1、在cmd中进入到nginx.exe所在的文件夹中。 2、执行命令:nginx.exe -c conf/nginx.conf 提示nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions),说明80端口被占用,改成其他端口即可(如8080)。 3、打开浏览器,输入localhost:8080,就可以访问Nginx搭建的Web服务器了。 总结: 以上就是在Windows操作系统下Nginx安装配置步骤。需要注意的是,配置文件中的选项对应的含义需要认真了解并修改,否则可能会造成配置错误或安全隐患。在运行之前还应当检查一下端口是否被占用,并选择一个空闲的端口,才可以正常启动Nginx服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值