ubuntu中使用nginx把本地80端口轉到其他端口
因為只是在開發的過程中遇到要使用域名的方式訪問, 而linux默認把1024以下的端口全部禁用.
在網上找了N多方式開啟80端口無果后, 方才想到使用代理的方式轉到其他端口. 自然而然就想到了
用Nginx, 但在配置過程中也是出現了各種奇葩問題, 先做個筆記省得之后忘了.
注: 本文只是在Nginx上做最簡單的端口跳轉.
安裝nginx
在ubuntu中安裝nginx比較簡單.
sudo apt-get install nginx
坐等安裝好即可.
或者也可以通過源碼安裝. 可參考: http://www.cnblogs.com/skynet/p/4146083.html
配置轉發
nginx的默認安裝路徑在/usr/local/nginx下.
nginx的默認配置在/etc/nginx下.
把80端口指向8080端口, 方法如下:
修改nginx.conf
- 注釋掉改行:
include /etc/nginx/conf.d/*.conf;
#include /etc/nginx/sites-enabled/*; //
- 在http配置項中增加如下內容:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
- 重啟nginx
sudo service nginx restart //或者 sudo nginx -s reload
- 然后就可以直接通過localhost/index.htm來訪問8080端口的項目了.
nginx簡單的操作命令
sudo service nginx start #啟動
sudo service nginx stop #停止
sudo service nginx restart #重新啟動
sudo service nginx reload #重新啟動
sudo nginx -s start #啟動
sudo nginx -s stop #停止
sudo nginx -s restart #重新啟動
sudo nginx -s reload #重新加載配置
