Nginx安装与使用

一、安装

1. 下载Nginx文件:http://nginx.org/en/download.html

2. 解压文件到指定目录

3. cmd指令启动

打开CMD,切换到解压目录。
输入:

1
2
3
nginx.exe 
或者
start nginx

常用指令

1
2
3
nginx -s stop          // 停止nginx
nginx -s reload // 重新加载配置文件
nginx -s quit // 退出nginx

4.检测是否启动成功

浏览器输入

1
http://localhost

显示Welcome to nginx!

部署成功

二、配置文件的使用

容器的配置都在nginx-1.13.10\conf\nginx.conf配置文件中设置
(新建server,进行端口和请求的监听)

(1)格式一(默认端口监听):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

server {
listen 80;#监听的端口
#域名可以有多个,用空格隔开
server_name localhost;

location / {
root html;#文件目录
index index.html index.htm;#静态文件
}
#请求错误时的请求页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}

(2)格式二(跨域转发):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 8089;
server_name localhost;
#对 "/" 启用反向代理
location /{
proxy_pass http://192.168.66.159:18188/;#需要转发的URL
add_header 'Access-Control-Allow-Origin' '*'; #添加请求头信息允许所有请求转发
#add_header 'Access-Control-Allow-Credentials' 'true';#当该标志为真时,响应于该请求是否可以被暴露
# add_header 'Access-Control-Allow-Methods' 'GET';#指定请求的方法,可以是GET,POST,PUT,DELETE,HEAD 同样支持通配符,如允许来自任何域的请求:
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

(3)格式三(默认访问静态页面+静态资源):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
server {
#请求方式判断
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
listen 8088;#监听的端口
#域名可以有多个,用空格隔开
server_name localhost;

location / {
root D:/项目文档/和利时/theme/templates/中海油/;#文件目录
index 中海油-报警信息查询.html;#静态页面
}
#请求错误时的请求页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
#静态资源访问路径设置
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|pdf|txt)
{
root D:/项目文档/和利时/theme/;
}
}

nginx.conf常用配置详解

1
https://www.jianshu.com/p/1b44b5142155
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2018-2020 丁振莹
  • 访问人数: | 浏览次数:

你的每一分支持,是我努力下去的最大的力量 ٩(๑❛ᴗ❛๑)۶

支付宝
微信