# Nginx 設定方法 :house: [回筆記首頁](https://www.mirai.tw) ###### tags: `Ngix` `Proxy` `Reverse` [toc] ## Nginx ### 1. 移除原來的Web server: ```systemctl status``` 查一下是否已安裝web server,若有,請先將他移除, 移除方法如下:(以apache2為例) ```linux! systemctl stop apache2 #關閉web server(apache2) systemctl disable apache2 #卸載web server(apache2) apt remove apache2 #移除web server(apache2) systemctl status apache #如果是centos系列將apt換成yum ``` ### 2. 安裝 Nginx: ```linux! apt install nginx -y #如果是centos系列將apt換成yum systemctl enable nginx #裝載web server(apache2) systemctl start nginx #開啟web server(apache2) systemctl status nginx #查看是否成功 ``` ### 3. 設定 Nginx: /etc/nginx/conf.d (這個是用來放Global設定檔的) /etc/nginx/sites_available (這個是用來放單一網站設定檔的) /etc/nginx/sites_enabled (這個是nginx會讀取的目錄) 然後用 symblic link 將available中設好的設定檔連過enabled中即可,不用時將enabled中的link移除就好了,不必把整個設定檔刪除 例如: 新增一個設定檔 server1 nano /etc/nginx/sites_available/server1 ln -s /etc/nginx/sites_available/server1 /etc/nginx/sites-enabled nginx -t #測試設定檔是否正確 systemctl restart nginx ``` #server1 server { listen 80; listen [::]:80; server_name server1.com app1.com; root /home/user1/www; index index.html; } ``` ### 4. 設定 Nginx的 reverse proxy:這裡如果寫2個upstream就能負載平衡 ``` # reverse proxy 的設定範例 server { listen 1122; # 在 public ip中開啟的 port - forward 到 server 1 proxy_pass inner-server1; } upstream inner-server1 { server 192.168.1.11:22; # 內網中server1 ip 與 sshd port } ``` 參考資料: [Nginx原生變數]: https://blog.wangqi.love/articles/nginx/nginx%E6%95%B4%E7%90%86.html ### 5.Nginx中rewrite的範例 使用 rewrite 將 $request_uri 改寫。 http://host/article/show?id=4&type=php&cat=12 rewrite到 http://host/article/show/4?type=php&cat=12 ``` if($request_uri ~* "/article/show\?id=([0-9]+)&(.*)") { set $myid $1 set $myque $2 rewrite ^/ /article/show/$myid?$$myque? redirect; } ``` ### 6.Nginx Server中的$uri轉成內網的port範例 http://host/port/api/path_info?query_string 外網中\$URI host.port pass(reverse proxy)to host:port (port:8000~8499) http://localhost:port/api/path_info?query_string 內網中app的\$URI ``` server { listen 80; listen [::]:80; server_name server_dn1 server_dn2; #這邊改成server的doamin name root /home/kevin/www; index index.html; location / { if ($request_uri ~* "/([8][0-4][0-9][0-9])/(.*)") { set $myport $1; set $myuri $2; proxy_pass http://127.0.0.1:$myport/$myuri; } } } ``` ### 6.Nginx Server開啟使用者網頁範例 http://host/port/api/path_info?query_string 外網中\$URI host.port pass(reverse proxy)to host:port (port:8000~8499) http://localhost:port/api/path_info?query_string 內網中app的\$URI ``` server { listen 80; listen [::]:80; server_name server_dn1 server_dn2; #這邊改成server的doamin name root /home/kevin/www; index index.html; location / { if ($request_uri ~* "/([8][0-4][0-9][0-9])/(.*)") { set $myport $1; set $myuri $2; proxy_pass http://127.0.0.1:$myport/$myuri; } } location ~ ^/~(.+?)(/.*)?$ { alias /home/$1/www$2; index index.html index.htm; autoindex on; } } ``` Author: - [name=林奇賢]
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.