Linux(centos7)安装nginx并配置代理
安裝并啟動nignx
通過yum進行安裝
[root@server ~]# yum install nginx [root@server ~]# service nginx start然后進入瀏覽器,訪問服務器ip,如果看到
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
恭喜你,你成功了!
如果不成功,檢查防火墻是否放過了80端口,放開80端口即可。
配置文件
當你執行nginx -t得時候,nginx會去測試你得配置文件得語法,并告訴你配置文件是否寫得正確,同時也告訴了你配置文件得路徑:
# nginx -t nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful反向代理
編輯nginx.conf文件
vim nginx.conf在文件中添加或修改:
server {listen 80; //監聽80端口server_name www.xxx.xxx; //請求地址location / {proxy_pass http://localhost:8081; //代理的地址和端口proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;} }這樣設置后,凡是www.xxx.xxx這個域名的請求都會代理到8081端口上。
可以在conf文件中有多個server,這樣就能實現不同的二級域名代理到不同的端口上。
重啟nginx
[root@server ~]# service nginx restart如果重啟出錯,如下:
重啟nginx出錯
Job for nginx.service failed because the control process exited with error code. See “systemctl status nginx.service” and “journalctl -xe” for details.
終端中輸入nginx -t命令就可以查看具體問題了。
日志
nginx日志地址為/var/log/nginx/目錄下
如果想下載日志到本地查看,可以:
scp root@[服務端ip]:/var/log/nginx/xxx.xxx [本地目錄,如:~/Downloads/]無法訪問
配置代理后依然訪問不了
這里指的是ping不通,報錯Unknown host的情況
這是因為代理的域名沒有加入DNS解析,在阿里云中添加二級域名DNS解析即可
添加完需要等一段時間才行
總結
以上是生活随笔為你收集整理的Linux(centos7)安装nginx并配置代理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux(CentOS)下安装mong
- 下一篇: 浅析在公众号中使用弛声sdk为什么上传解