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文件夾下,可以查看相關的日誌