nginx 介绍以及使用
目录
一、nginx 介绍
nginx 是一个HTTP和反向代理服务器,支持邮件的代理和通用的TCP/UDP的代理,可以自己在国外一个服务器做代理,然后我们可以访问国外的一台服务器,然后代理访问所有的国外的网站了。
二、nginx 简单使用
下载地址:http://nginx.org/en/download.html;下载对应到的安装包,解压出来(解压即安装),然后双击nginx.exe,启动服务。然后你再浏览器输入:http://localhost出现以下结果就是nginx.exe成功了。
三、nginx 自定义使用,即自己定义代理细节
修改配置,在解压出来的conf文件夹下。找到nginx.conf文件。打开修改成自己的代理url,我是修改是这样的。如图:配置文件就是这样的:
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root D:/ideaProjectUI/dist;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
#1
location /auth/{
proxy_pass http://localhost:9999;
}
#2
location /admin/{
proxy_pass http://localhost:9999;
}
#3
location /code/{
proxy_pass http://localhost:9999;
}
#4
location /gen/{
proxy_pass http://localhost:9999;
}
#5
location /file/{
proxy_pass http://localhost:9999;
}
}
}
}
1、server:是一个服务
2、listen:服务监听的接口
3、server_name:服务名称
4、location一个URL转发的路径,location后面的“/”代表的是url的默认转发,所以4中的意思是默认转发到D:/ideaProjectUI/dist文件夹的index.html文件。代理成功如图:
5、location后面的“/auth/”代表的是url的后一位带有“/auth/”就转发到http://localhost:9999地址上。
修改配置启动nginx服务的注意点:
特别注意不要直接双击打开nginx.exe文件,修改配置后,这种方式是无效的。需要cmd命令行方式启动:
(1)首先在任务管理器上关掉nginx相关的服务,
(2)然后使用以下命令启动,关闭,重启nginx:
shart nginx :启动nginx
nginx -s reload :修改配置文件重新加载启动
nginx -s quit :停止关闭nginx
nginx -s reopen :查看日志,日志在安装的文件夹的log文件夹下,可以查看相关的日志