1、先下载直接去官网nginx.org,下载一个Windows下的全新版本,下载地址http://nginx.org/en/download.html如图:
2、下载完成后解压
3、下载完成后,解压缩,运行cmd,使用命令进行操作,不要直接双击nginx.exe(双击nginx.exe,这样会导致修改配置后重启、停止nginx无效)
4、进入nginx的解压缩后的目录
如:E:\software\nginx-1.22.1
5、启动nginx服务,启动时会一闪而过是正常的
start nginx
6、查看任务进程是否存在,打开任务管理器
7、修改配置文件,进入解压缩目录,在conf目录下找到nginx.conf打开即可,找到server这个节点,修改端口号,如果有需求可以修改主页目录没有就不用修改(修改配置在后面第11点)
8、修改完成后保存,使用以下命令检查一下配置文件是否正确,后面是nginx.conf文件的路径,successful就说明正确了
nginx -t -c conf/nginx.conf
9、重启nginx
nginx -s reload
打开浏览器访问刚才的域名及端口http://localhost:8800,出现欢迎页就说明部署成功了
10、关闭nginx服务
nginx -s stop
11、nginx跨域配置,修改conf目录下的nginx.conf文件
1)需要过滤的地址
map $http_origin $corsHost {
default 0;
"~http://192.168.43.135:8800" http://192.168.43.135:8800;
"~http://192.168.39.118:8800" http://192.168.39.118:8800;
}
2)配置跨域和端口号
server {
#listen 80;#默认端口
listen 8800;#修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root C:/Users/xxx/Desktop;#指定访问路径
index index.html index.htm;
# 允许 所有头部 所有$corsHost域 所有方法
add_header 'Access-Control-Allow-Origin' $corsHost;
# 允许 所有头部 所有域 所有方法
#add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
# OPTIONS 直接返回204
if ($request_method = 'OPTIONS') {
return 204;
}
#proxy_pass http://192.168.43.135:8800; # 转发规则
#proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
}
12、重启,可访问C:/Users/xxx/Desktop下的文件,如打开浏览器访问刚才的域名及端口http://localhost:8800/mytest/images/meinv.jpeg
http://localhost:8800/mytest/images/meinv.jpeg
附:完整的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;
map $http_origin $corsHost {
default 0;
"~http://192.168.43.135:8800" http://192.168.43.135:8800;
"~http://192.168.39.118:8800" http://192.168.39.118:8800;
}
server {
#listen 80;#默认端口
listen 8800;#修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
root C:/Users/xxx/Desktop;#指定访问路径
index index.html index.htm;
# 允许 所有头部 所有$corsHost域 所有方法
add_header 'Access-Control-Allow-Origin' $corsHost;
# 允许 所有头部 所有域 所有方法
#add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' '*';
add_header 'Access-Control-Allow-Methods' '*';
# OPTIONS 直接返回204
if ($request_method = 'OPTIONS') {
return 204;
}
#proxy_pass http://192.168.43.135:8800; # 转发规则
#proxy_set_header Host $proxy_host; # 修改转发请求头,让8080端口的应用可以受到真实的请求
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
# }
#}
}